gpt4 book ai didi

ruby-on-rails - ActiveRecord 在简单创建时引发重复键错误

转载 作者:行者123 更新时间:2023-12-04 03:41:42 25 4
gpt4 key购买 nike

由于我不明白的原因,我正在违反唯一性。

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

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

当我运行它时,ActiveRecord 抛出这个异常:
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"

即使我的代码没有指定 Price 的主键是什么
对象应该是,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

关于ruby-on-rails - ActiveRecord 在简单创建时引发重复键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21028997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com