迁移中没有自动增量选项的 id 字段 [英] id field without autoincrement option in migration
问题描述
我有一个像这样的数据库迁移:
I have a DB migration like so:
class CreateParticipations < ActiveRecord::Migration
def self.up
create_table(:participations, :primary_key => 'Seat') do |t|
t.integer :Seat
t.string :Nickname
t.string :Clan
t.string :FirstName
t.string :LastName
t.string :Email
t.boolean :Payed
t.timestamps
end
end
def self.down
drop_table :participations
end
end
现在,使用自动增量创建座位.但是,我不希望那样.我想要它没有自动增量.我将在我的逻辑中定义自己的座位.
Now, seat is created with an Auto increment. However, I do not want that. I want it without an auto increment. I will define Seat myself in my Logic.
我一直在环顾四周,但找不到如何禁用 auto_increment.
I have been looking around but I cannot find how to disable auto_increment.
我该怎么做?除了在 MySQL 中手动完成.
How do I do this? Except for manually doing it in MySQL.
推荐答案
为了记录,如果您绝对需要这样做(它不应该经常发生),这里是使用Rails 迁移 DSL:
For the record, if you absolutely need to do this (it shouldn't happen often), here's the way to do a non-autoincrementing primary key with the Rails migration DSL:
create_table(:table_name, :id => false) do |t|
t.integer :id, :options => 'PRIMARY KEY'
end
无论如何这都适用于 MySQL,如果您的数据库使用不同的语法来指定主键,请替换 :options =>'PRIMARY KEY'
和任何东西.
That will work for MySQL anyway, if your DB uses different syntax to specify a primary key, replace the :options => 'PRIMARY KEY'
with whatever that is.
这篇关于迁移中没有自动增量选项的 id 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!