自定义非自动递增的ActiveRecord" ID"列可能吗? [英] Custom non auto-incrementing ActiveRecord "id" column possible?

查看:97
本文介绍了自定义非自动递增的ActiveRecord" ID"列可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用自定义的ID(而不是自动递增默认的)为Rails模型。基本上,所有标识的iTunes商店的ID,这只是长整数。是否有可能关闭默认自动递增的ID,并要求之一进行设置?这些ID也将被用作其他模型外键。

I'd like to be able to use a custom id (instead of the auto-incrementing default ones) for a Rails model. Basically, all the ids will be iTunes store ids, which are just long integers. Is it possible to turn off the default auto-incrementing ids and require that one be set? These ids will also be used as foreign keys in other models.

推荐答案

事情是这样的:

create_table :blah, {:id => false} do |t|
  t.int :my_custom_int_id
end
execute "ALTER TABLE blah ADD PRIMARY KEY (my_custom_int_id);"

这篇关于自定义非自动递增的ActiveRecord" ID"列可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆