简体   繁体   English

如何在 Rails Active Record 中关闭 auto_increment

[英]How to turn off auto_increment in Rails Active Record

Is it possible to create primary key without auto_increment flag in ActiveRecord ?是否可以在ActiveRecord 中创建没有auto_increment标志的主键?

I can't do我做不到

create table :blah, :id => false

because I want to have primary key index on the column.因为我想在列上有主键索引。 I looked up documentation but didn't find anything useful.我查找了文档,但没有找到任何有用的信息。

Is it possible to create primary key without auto_increment?是否可以在没有 auto_increment 的情况下创建主键?

Try this?试试这个?

create_table(:table_name, :id => false) do |t|
  t.integer :id, :options => 'PRIMARY KEY'
end

Okay, the question is old and the OP did not specify versions.好的,问题很旧,OP 没有指定版本。 None of the answers given here worked for me with these versions:对于这些版本,这里给出的答案都不适用于我:

mysql2 0.3.11
rails 3.2.13 
mysql 5.5

I ended up going for this:我最终去了这个:

class SomeMigration < ActiveRecord::Migration
  # emulate a primary_key column without auto-increment
  # the solution here is to use a non-null integer id column with a unique index
  # this is semantically different from PRIMARY KEY in mysql but not
  # _too_ functionally different, the only difference is that mysql enforces
  # no-more-than-one-primary-key but allows >1 unique index
  def up
    create_table :foobars, :id => false do |t|
      t.integer :id, :null => false
      t.string :name
    end
    add_index :foobars, :id, :unique => true
  end
end

I hope that saves someone out there from spending time tracking this down, or worse ... using the answer without checking what it does to the db ... because the result of using either sojourner's or jim's answers (with my versions of the dependencies) is that the migration runs fine but NULL ids are allowed, and duplicate ids are allowed.我希望可以避免有人花时间跟踪这个问题,或者更糟……使用答案而不检查它对数据库的作用……因为使用 sojourner 或 jim 的答案的结果(使用我的依赖版本) 是迁移运行良好,但允许 NULL id,并且允许重复的 id。 I did not try Shep's answer because I don't like the idea of db/schema.rb being inconsistent (+1 to Shep for being explicit about that shortcoming, sometimes that'd be a Bad Thing)我没有尝试 Shep 的答案,因为我不喜欢 db/schema.rb 不一致的想法(Shep 明确指出该缺点,有时这是一件坏事)

I'm not sure the significance of this, but with this solution, mysql describe shows it as a primary key, same as an AR table with default :id ... as in:我不确定这的重要性,但是使用此解决方案,mysql describe显示为主键,与具有默认值的 AR 表相同 :id ... 如下所示:

table with AR default :id带有 AR 默认值的表:id

+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| id                  | int(11)      | NO   | PRI | NULL    | auto_increment |

table with my solution:我的解决方案表:

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | NO   | PRI | NULL    |       |

which is sort of interesting because the SQL generated by the migration with my solution does not include "PRIMARY KEY" (of course) ... but with AR default :id it does ... so it seems mysql, at least for describe treats a non-null unique-indexed key as a primary key这是种有趣的,因为迁移我的解决方案生成的SQL不包括“主键”(当然)...但AR默认:ID它......如此看来mysql的,至少describe对待一个非空的唯一索引键作为主键

HTH someone某人

That didn't work for me, but the following did:这对我不起作用,但以下内容确实有效:

create_table(:table_name, :id => false) do |t|
  t.column :id, 'int(11) PRIMARY KEY'
end

Only problem is that you lose it in the schema.rb.唯一的问题是你在schema.rb 中丢失了它。

You can create a table like this:您可以像这样创建一个表:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :routers, { id: false } do |t|
      t.integer :id
    end

    execute "ALTER TABLE routers ADD PRIMARY KEY (id);"
  end
end

And that really works in Rails 4.0.2 and Postgresql 9.3.2.这在 Rails 4.0.2 和 Postgresql 9.3.2 中确实有效。

To disable auto increment as of Rails 5 you can simply pass要从 Rails 5 开始禁用自动增量,您可以简单地通过

default: nil

for instance例如

create_table :table_name, id: :bigint, default: nil do |t|
  # ... fields ...
end
  def change
    create_table :tablename do |t|
      # t.string :fieldname
    end

   change_column :tablename, :id, :bigint, auto_increment: false
 end

Notice: Since Rails 5.1 default primary keys are bigint.注意:由于 Rails 5.1 默认主键是 bigint。 http://www.mccartie.com/2016/12/05/rails-5.1.html http://www.mccartie.com/2016/12/05/rails-5.1.html

If you want 4-byte key change :bigint to :integer如果你想要 4 字节的密钥更改 :bigint 到 :integer

Is it possible to create primary key without auto_increment flag in ActiveRecord ?是否可以在ActiveRecord中创建没有auto_increment标志的主键?

I can't do我做不到

create table :blah, :id => false

because I want to have primary key index on the column.因为我想在列上有主键索引。 I looked up documentation but didn't find anything useful.我查阅了文档,但没有发现任何有用的信息。

Is it possible to create primary key without auto_increment?是否可以在没有auto_increment的情况下创建主键?

文章回答了这个问题最好的,因为所有上述方法将打破任何references您可能由于整数类型不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM