rails3 bigint 主键 [英] rails3 bigint primary key

查看:67
本文介绍了rails3 bigint 主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Rails 3 下创建一个 bigint(或 string 或任何不是 int 的)类型的主键字段.

I would like to create a bigint (or string or whatever that is not int) typed primary key field under Rails 3.

我有一个给定的数据结构,例如:

I have a given structure of data, for example:

things
------
id bigint primary_key
name char(32)

我目前尝试采用的方法:

The approach I'm currently trying to push:

create_table :things, :id => false do |t| # That prevents the creation of (id int) PK
  t.integer :id, :limit => 8 # That makes the column type bigint
  t.string :name, :limit => 32
  t.primary_key :id # This is perfectly ignored :-(
end

列类型是正确的,但主键选项不会出现在 sqlite3 中,我怀疑 MySQL 也是这种情况.

The column type will be correct, but the primary key option will not be present with sqlite3 and I suspect that this is the case for MySQL too.

推荐答案

我自己不久前就有了,并在这里找到了答案:使用 Rails,如何将主键设置为不是整数类型列?

Had that myself not long ago and found the answer here: Using Rails, how can I set my primary key to not be an integer-typed column?

在执行迁移之前,您需要设置 primary_key: false 并使用自定义语句.

You need to set primary_key: false and then use a custom statement before you execute the migration.

编辑 1:您需要检查您的数据库文档以获取要执行的确切查询.它作为常规 SQL 语句执行并且需要特定于数据库.我提到的问题中的示例适用于 Postgre SQL.如果您使用 MySQL,则可能需要更改它.

EDIT 1: You need to check your database docs for the exact query to perform. It is executed as a regular SQL statement and needs to be database specific. The example in the question I referred to is for Postgre SQL. If you are on MySQL you might have to change that.

这篇关于rails3 bigint 主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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