带有 UUID 主键的 ActiveRecord 迁移
在我要创建的迁移中,表的主键是一个名为“id”的字段,但它不是一个自动递增的整数。它的数据类型应该是唯一标识符(uuid)。这是我尝试过的:
create_table :some_things, :id => false do |t|
t.column :id, :uniqueidentifier, :primary => true
t.column :name, :string, :limit => 255
t.column :type, :tinyint
t.column :deleted_flag, :bit
t.column :class_id, :uniqueidentifier
t.timestamps
end
这可以正常创建表,但没有主键(因为我说:id=>false)。如果我说“create_table :some_things, :id => true, :primary => :id”,那么“id”成为主键,但它是一个自增整数,而不是非自增uuid 。
如何使此迁移工作,以便主键是“uniqueidentifier”类型(非自动递增)的名为“id”的字段?
我正在使用: SQL Server 2008, Rails/ActiveRecord 3.0.3, activerecord-sqlserver-adapter gem, 和 ODBC 连接。
In the migration that I want to create, the primary key of the table is a field called "id" but it is not an auto-incrementing integer. It's datatype should be uniqueidentifier (a uuid). Here is what I have tried:
create_table :some_things, :id => false do |t|
t.column :id, :uniqueidentifier, :primary => true
t.column :name, :string, :limit => 255
t.column :type, :tinyint
t.column :deleted_flag, :bit
t.column :class_id, :uniqueidentifier
t.timestamps
end
This creates the table alright, but there is no primary key (because I said :id=>false). If I said "create_table :some_things, :id => true, :primary => :id", then "id" becomes the primary key, but it is an auto-incrementing integer, not a non-auto-incrementing uuid.
How can I make this migration work so that the primary key is a field called "id" of type "uniqueidentifier" (non-auto-incrementing)?
I'm using:
SQL Server 2008,
Rails/ActiveRecord 3.0.3,
the activerecord-sqlserver-adapter gem,
and an ODBC connection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道如何直接解决问题,但我有一个解决方法。
输入不带“primary”指令的迁移列 ID。在迁移中的方法“create_table”之后执行 SQL 的添加约束
(不要使用 MSSQL,并且 SQL 语法可能会出错)
在模型中通过添加
或定义主键
I don't know how to solve problem directly, but I've got a workaround.
Put in your migration column id without 'primary' directive. And after method 'create_table' in migration execute SQL's add constraint
(don't use MSSQL and may be mistake in SQL-syntax for it)
In your model define primary key by adding
or
以下是我解决这个问题的方法:
1) 在迁移中,我允许迁移自动生成 id 和 id_sequence,并添加一个虚拟 uuid 列(此处称为 guid)。这只是开发道路上最简单的方法。所以
我使用迁移
2) 迁移后,我可以通过 sql 客户端运行以下命令
3) 我使用两个 gem 来帮助解决这个问题
显然,我的解决方案是针对 Postgres DB ...但我发布这个是因为它似乎与你的问题之一相关,即你如何使用 Rails 来保持数据库的距离?无论如何,UUIDtools 与数据库无关。
4)在我的 Thing 类中,我使用这个
,其中 UUID 只是一个像这样的模块
顺便说一句,我在这个要点中找到了后者:
https://gist.github.com/rmoriz/937739
但我的解决方案有点不同。
Here was how I solved this problem:
1) In my migration, I allowed the migration to autogenerate the id and the id_sequence, and added a dummy uuid column (called guid here). It was simply the easiest way to go in the development path. So for
I use migration
2) After the migration, I can run the following through a sql client
3) I am using two gems to help with this
Clearly, my solution is against a Postgres DB ... but I post this because it seems relevant to one of your issues, namely how do you use Rails to keep the db at arms length? At any rate UUIDtools is db agnostic.
4) In my Thing class I use this
where UUID is simply a module like so
By the way, I found the latter in this gist:
https://gist.github.com/rmoriz/937739
But my solution is a little different.