gpt4 book ai didi

ruby-on-rails - Rails:PG::NotNullViolation:错误:列 "id"中的空值违反了非空约束

转载 作者:行者123 更新时间:2023-11-29 11:24:12 24 4
gpt4 key购买 nike

这是在 rails 3.2.12 和 pg 9.3 上将记录保存到 postgres 数据库时的错误:

ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "sw_module_infox_module_infos" ("about_controller", "about_init", "about_log", "about_misc_def", "about_model", "about_onboard_data", "about_subaction", "about_view", "about_workflow", "active", "api_spec", "category_id", "created_at", "last_updated_by_id", "module_desp", "name", "submit_date", "submitted_by_id", "updated_at", "version", "wf_state") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) RETURNING "id"):
activerecord (3.2.12) lib/active_record/connection_adapters/postgresql_adapter.rb:1166:in `get_last_result'
activerecord (3.2.12) lib/active_record/connection_adapters/postgresql_adapter.rb:1166:in `exec_cache'

该表到目前为止工作正常(今天出错前保存了大约 50 条记录)。在 pgadmin 中打开 pg 表后。我们发现表中的 idinteger。我们还发现其他表上的 Idserial。看来 id 应该是 serial 这样它就可以自动递增了。如果是,那么如何将 id 列从 integer 转换为 serial?如果不是,那么如何解决这个问题?

最佳答案

The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases).

如果您的列是一个整数,您不能将它转换序列,但您可以模仿,PostgreSQL 会做的,只是就像您使用序列号创建表格一样:

CREATE SEQUENCE tablename_colname_seq;ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename_colname_seq'::regclass);ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

关于ruby-on-rails - Rails:PG::NotNullViolation:错误:列 "id"中的空值违反了非空约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24298171/

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