将记录保存到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表后。我们发现id
桌上的是integer
。我们还发现Id
其他表是serial
。似乎ID应该是serial
可以自动递增的。如果是,那么如何将id列转换为serial
from integer
?如果不是,那么如何解决此问题?
的数据类型
smallserial
,serial
并且bigserial
不是真正的类型,而仅仅是用于创建的唯一标识符列(类似于一个标记方便AUTO_INCREMENT
通过一些其它数据库支持的属性)。
如果您的列是integer
,则不能将其转换为serial
,但是可以模仿PostgreSQL会执行的操作,就像您使用序列创建表一样:
CREATE SEQUENCE tablename_colname _seq; ALTER TABLE表名ALTER COLUMN列名SET DEFAULT nextval(' tablename_colname _seq':: regclass); ALTER SEQUENCE tablename_colname _seq OWNED BY tablename.colname ;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句