活动记录在简单创建时引发重复键错误



由于我不明白的原因,我遇到了唯一性违规。

我有一个用于跟踪各种股票对象价格的表格。 我创建或在给定日期更新给定净值的价格,如下所示:

p = Price.where(equity_id: eq.id, date: date).first_or_create

当我运行这个时,活动记录会抛出以下异常:

ActiveRecord::RecordNotUnique Exception: PG::UniqueViolation: ERROR:
duplicate key value violates unique constraint "prices_pkey" DETAIL: Key
(id)=(4273819) already exists.
: INSERT INTO "prices" ("created_at", "date", "equity_id", "updated_at")
VALUES ($1, $2, $3, $4) RETURNING "id"

即使我的代码没有做任何事情来指定价格的主键是什么对象应该是,ActiveRecord似乎坚持重用现有的id---in这个案例,4273819。

如果有帮助,这里是相当冗长的回溯:

from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:780:in `exec_cache'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:139:in `block in exec_query'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:183:in `exec_insert'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:96:in `insert'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/relation.rb:76:in `insert'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/persistence.rb:498:in `create_record'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:78:in `create_record'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `block in create_record'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__2672223936792095331__create__callbacks'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:303:in `create_record'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/timestamp.rb:57:in `create_record'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/persistence.rb:466:in `create_or_update'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `block in create_or_update'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:383:in `_run__2672223936792095331__save__callbacks'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:299:in `create_or_update'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/persistence.rb:106:in `save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/validations.rb:51:in `save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:32:in `save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in `block (2 levels) in save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:270:in `block in save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/transactions.rb:269:in `save'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/protected_attributes-1.0.3/lib/active_record/mass_assignment_security/persistence.rb:46:in `create'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/relation.rb:121:in `block in create'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/relation.rb:121:in `create'
from /home/ded/.rvm/gems/ruby-2.0.0-p353@s16/gems/activerecord-4.0.0/lib/active_record/relation.rb:133:in `first_or_create'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:130:in `block (3 levels) in digest_xch'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:96:in `each'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:96:in `block (2 levels) in digest_xch'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:95:in `open'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:95:in `block in digest_xch'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:87:in `chdir'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:87:in `digest_xch'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:69:in `block in digest'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:68:in `each'
from /home/ded/src/s16-o-r/app/models/price_retriever.rb:68:in `digest'
from /home/ded/src/s16-o-r/app/models/runner.rb:164:in `digest'
from /home/ded/src/s16-o-r/app/models/runner.rb:85:in `run'
from /home/ded/src/s16-o-r/app/models/runner.rb:54:in `block in catch_up'
from /home/ded/src/s16-o-r/app/models/runner.rb:39:in `each'
from /home/ded/src/s16-o-r/app/models/runner.rb:39:in `catch_up'
from script/run.rb:15:in `<main>'

对于 Postgres 和 Rails 5,通过将以下内容放入 rake 任务来重新同步数据库:

ActiveRecord::Base.connection.tables.each do |t|
  ActiveRecord::Base.connection.reset_pk_sequence!(t)
end

听起来您的数据库不同步,除非显式设置,否则它会处理 ID 分配。有关如何重新同步,请参阅以下答案。

当postgres不同步时如何重置它的主键序列?

存在具有相同equity_id的记录,并且可能是您在模型中为equity_id添加了唯一性约束Price

最新更新