开发者

Rails won't use sequence for primary key?

For one reason or another the pre-existing Postgres schema I'm using with my Rails app doesn't have a default sequence set for a table's primary key, so I am required to query for it every time I want to create a new row.

I have set_sequence_name "seq_people_id" in my model, but wh开发者_StackOverflow社区enever I call Person.new Postgres complains to me because Rails is executing the insert query without the ID (which is marked as NOT NULL in the schema).

How do I tell Rails to always use the sequence when creating new records?

  • Postgres 8.1.4
  • ActiveRecord 3.0.3
  • Rails 2.3.10


Here's what I get when I run psql and \d foo:

                                 Table "public.foo"


 Column |   Type        |              Modifiers                       
--------+---------------+------------------------------------------------------
  id    | integer       | not null default nextval('foo_id_seq'::regclass)
(etc.)

I'd check the following:

  • Verify the actual sequence name is the same as what you reference (people_id_seq vs. seq_people_id)
  • Verify the table's default is similar to what I have above
  • (just checking) is the primary key's field named "id" ?
  • Did you create the table using a migration or by hand? If the latter, try creating a table with a migration, specifying the same fields as in your people table. Does it work properly? Compare the tables.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜