2013-03-27 59 views
-4

在使用rake在我的pgslq数据库上创建表时,rails allways创建一个“id”字段作为主键。Rails(rake)默认ID字段

我的问题是,我需要有一个自定义名称的ID字段。我如何才能指定数据库字段只有我想要的字段,指定主键?

+4

这是覆盖在[多](http://stackoverflow.com/questions/9986658/setting-primary-key -for-a-database-not-named-id)[other](http://stackoverflow.com/questions/1200568/using-rails-how-can-i-set-my-primary-key-to-not -be-an-integer-typed-column)堆栈溢出问题和[all the web](http://roninonrails.blogspot.com/2008/06/using-non-standard-primary-keys-with.html) ,但我只是补充说它会让你的生活变得糟糕,你应该尽一切力量在可能的情况下切换到表中使用'id'。这是Rails的主要缺点,但'id'使事情变得简单。 – 2013-03-27 13:24:12

+0

Downvote for not so so before posting a redundant question。 – 2013-03-27 13:25:27

+0

我做了一个快速搜索,并没有发现任何东西。感谢关于不使用带缺省名称的ID的问题的额外信息...问题是因为我们有数据模型,现在我们正在讨论它。 – Techmago 2013-03-27 14:00:37

回答

3

可以指定主键迁移传递它的名字create_table

create_table :my_table, :primary_key => :custom_id do |t| 
    #... 
end