【发布时间】:2013-04-03 21:34:48
【问题描述】:
不知何故,我的架构以这个 id 列结束:
create_table "tables", :id => false, :force => true do |t|
t.integer "id", :null => false
# other fieds
end
这是列上的信息(注意主要是假的):
[4] pry(main)> Table.columns.first
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f98d06cc0f8
@coder=nil,
@default=nil,
@limit=nil,
@name="id",
@null=false,
@precision=nil,
@primary=false,
@scale=nil,
@sql_type="integer",
@type=:integer>
所以每当我尝试保存新记录时都会出现此错误:
ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint
恢复主键的最佳方法是什么?
【问题讨论】:
-
你能详细解释一下这是怎么发生的吗?您创建了一个没有
id的表,然后想要添加一个? -
我不知道它是怎么发生的。我查看了迁移,它没有明确设置此表的 id 字段。
-
现在要删除它吗?
-
另外,您可能想重新考虑使用表名
tables,因为它可能在 postgres 的保留关键字中。也许有更多经验的人会对此多说一些 -
“tables”这个名字只是这个问题的假名。
标签: ruby-on-rails ruby-on-rails-3 activerecord