How to turn off auto_increment in Rails Active Record

筅森魡賤 提交于 2019-11-27 03:54:39

问题


Is it possible to create primary key without auto_increment flag in ActiveRecord?

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?


回答1:


Try this?

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



回答2:


Okay, the question is old and the OP did not specify versions. 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. 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)

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:

table with AR default :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

HTH someone




回答3:


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.




回答4:


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.




回答5:


To disable auto increment as of Rails 5 you can simply pass

default: nil

for instance

create_table :table_name, id: :bigint, default: nil do |t|
  # ... fields ...
end



回答6:


In Rails 5 you can do

create_table :blah, id: :integer do |t|

If you want to change the name of primary key column pass primary_key parameter:

create_table :blah, id: :integer, primary_key: :my_awesome_id do |t|

See create_table documentation.




回答7:


  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. http://www.mccartie.com/2016/12/05/rails-5.1.html

If you want 4-byte key change :bigint to :integer



来源:https://stackoverflow.com/questions/1585323/how-to-turn-off-auto-increment-in-rails-active-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!