开发者

Issue sending an explicit primary key value to Model.create in rails v3

开发者 https://www.devze.com 2023-01-20 02:11 出处:网络
So I am trying to pass a primary key (id) value to my create method 开发者_如何学Pythonand it doesn\'t work.

So I am trying to pass a primary key (id) value to my create method 开发者_如何学Pythonand it doesn't work.

Is there a way to do this in rails?

Category.create!(:id => 2828, :name => 'Some Category')


I have similar problem. I have a table with primary_key called page_id I tried to do

SomeClass.create :page_id => 123

and

SomeClass.create :id => 123

and

obj = SomeClass.new :page_id => 123
obj.save!

and

obj = SomeClass.new :id => 123
obj.save!

with no luck.

Anyway, the solution was

obj = SomeClass.new :title => 'title', :whatever_field => 'whaver value'
obj.id = 123
obj.save!

Not sure why it works, but it is. Just call id accessor explicit. Note, the PRIMARY KEY in database is still page_id (I don't have id field at all)

Hope this helps.

0

精彩评论

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