gpt4 book ai didi

ruby-on-rails - Rails:delete_all 引发 PG::NotNullViolation

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

我正在将包含几百行的 CSV 导入到我的 rails 数据库中。

有时用户想要强制覆盖数据,所以我认为最好销毁所有数据并重新开始。

就像是:

account.catalog_listings.delete_all if should_refresh

CSV.foreach(file, options) do |row|
account.catalog_listings.create!({...rowstuff})

问题是 delete_all线路正在引发 PG 错误
ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "account_id" violates not-null constraint
DETAIL: Failing row contains (1, null, ... ... ).
: UPDATE "catalog_listings" SET "account_id" = NULL WHERE "catalog_listings"."account_id" = $1):
app/models/catalog_listing.rb:41:in `import_catalog_listings'
app/controllers/accounts_controller.rb:20:in `catalog'

有一个 null: false在几个外键字段上,但我不知道为什么 delete_all试图删除外键而不是删除整个记录?

更新 - 当我改变时一切正常:
account.catalog_listings.delete_all if should_refresh
到:
account.catalog_listings.destroy_all if should_refresh
除了 destroy遍历每个项目并一一删除:
  SQL (0.1ms)  DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1  [["id", 957]]
SQL (0.1ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 958]]
SQL (0.1ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 959]]
SQL (0.1ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 960]]
SQL (0.1ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 961]]
SQL (0.1ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 962]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 963]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 964]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 965]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 966]]
SQL (0.3ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 967]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 968]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 969]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 970]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 971]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 972]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 973]]
SQL (0.3ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 974]]
SQL (0.2ms) DELETE FROM "catalog_listings" WHERE "catalog_listings"."id" = $1 [["id", 975]]

不酷......有人知道更好的方法吗?

最佳答案

尝试添加 dependent: :destroycatalog_listings您的关联 Account模型。

https://apidock.com/rails/ActiveRecord/Associations/CollectionProxy/delete_all

关于ruby-on-rails - Rails:delete_all 引发 PG::NotNullViolation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44664327/

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