gpt4 book ai didi

ruby-on-rails - 插入新行 'PG::NotNullViolation: ERROR: null value in column "id 时出现 Postgresql 错误“违反非空约束”

转载 作者:行者123 更新时间:2023-11-29 13:21:59 25 4
gpt4 key购买 nike

我只是将我的美国服务器迁移到 heroku 上的欧盟区域,并将旧的数据库备份恢复到新服务器。不幸的是,我在尝试 insert an new row to one of table

时遇到了 error
INSERT INTO "users" ("created_at", "id", ...) VALUES ($1, $2, ...) [["created_at", Thu, 13 Oct 2016 23:53:17 CEST +02:00], ["id", nil], ...]

ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint

DETAIL: Failing row contains (null, ...)

我的架构如下所示

create_table "users", :id => false, :force => true do |t|
t.integer "id", :null => false
t.datetime "created_at", :null => false
...
end

我该如何解决这个问题?

最佳答案

我已经尝试了以下解决方案来解决这个问题,我正在写下那些需要它的人。

解决方案#1

删除我的本地数据库

rake db:drop

运行迁移

rake db:migrate

现在使用--data-only 选项导入生产数据库

pg_restore --data-only --verbose --no-acl --no-owner -h localhost -U user_name -d database_name latest.dump

它部分解决了我的问题,但丢失了表中的所有数据,所以这不是合适的

解决方案#2(工作正常)

观察错误后,我明白了一些事情,比如主键序列被破坏,系统不应该尝试向 ID 字段插入空值,还有一些我的模型忘记了主键。然后我通过模型设置主键,它的工作正常

class User < ActiveRecord::Base
self.primary_key = 'id'
end

关于ruby-on-rails - 插入新行 'PG::NotNullViolation: ERROR: null value in column "id 时出现 Postgresql 错误“违反非空约束”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40122351/

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