Rails 4 MySQL bigInt主键问题和错误
我需要在rails 4.1.8应用程序中使用14位的bigInt作为主键。以SO上的旧帖子为指导,我提出了以下解决方法Rails 4 MySQL bigInt主键问题和错误,mysql,ruby-on-rails,activerecord,rails-migrations,ruby-on-rails-4.1,Mysql,Ruby On Rails,Activerecord,Rails Migrations,Ruby On Rails 4.1,我需要在rails 4.1.8应用程序中使用14位的bigInt作为主键。以SO上的旧帖子为指导,我提出了以下解决方法 class CreateAcctTransactions < ActiveRecord::Migration def change create_table "acct_transactions", :id => false do |t| t.integer :id, :limit => 8,null
class CreateAcctTransactions < ActiveRecord::Migration
def change
create_table "acct_transactions", :id => false do |t|
t.integer :id, :limit => 8,null: false
t.integer "account_id",limit: 8,null: false
t.integer "transaction_type_id", null: false
t.datetime "date",null: false
t.text "description",limit: 255
t.decimal "amount",precision: 10, scale: 2, null: false
end
end
end
迁移运行正常,但表不会种子,id不是主id。我犯了错误吗?还是有更好的方法
非常感谢我通过编写SQL执行迁移来修复它,如下所示:
class CreateAcctTransactions < ActiveRecord::Migration
def self.up
# create ACCT_TRANSACTIONS table
create_table "acct_transactions", id: false, force: true do |t|
t.integer "id", limit: 8, null: false
t.timestamp "date", null: false
t.text "description", limit: 255
t.decimal "amount", precision: 10, scale: 2, null: false
t.integer "account_id", limit: 8, null: false
t.integer "transaction_type_id", null: false
end
execute "ALTER TABLE acct_transactions ADD PRIMARY KEY (id);"
add_index "acct_transactions", ["account_id"], name: "fk_acct_transactions_accounts1_idx", using: :btree
add_index "acct_transactions", ["date", "id"], name: "BY_DATE", using: :btree
add_index "acct_transactions", ["transaction_type_id"], name: "fk_acct_transactions_transaction_types1_idx", using: :btree
end
def self.down
drop_table :acct_transactions
end
end
class CreateAcctTransactions
注意第12行的执行语句。当我在那里的时候,我也把“日期”字段改成了时间戳,我本来打算这么做的。
它不漂亮,违反了“惯例”,但它工作得很完美,所以我可以继续。感谢您的关注。只有在运行迁移的情况下,才有可能重复此操作,而不是在使用schema.rb创建数据库的情况下(例如,如果运行rake db:setup)。
class CreateAcctTransactions < ActiveRecord::Migration
def self.up
# create ACCT_TRANSACTIONS table
create_table "acct_transactions", id: false, force: true do |t|
t.integer "id", limit: 8, null: false
t.timestamp "date", null: false
t.text "description", limit: 255
t.decimal "amount", precision: 10, scale: 2, null: false
t.integer "account_id", limit: 8, null: false
t.integer "transaction_type_id", null: false
end
execute "ALTER TABLE acct_transactions ADD PRIMARY KEY (id);"
add_index "acct_transactions", ["account_id"], name: "fk_acct_transactions_accounts1_idx", using: :btree
add_index "acct_transactions", ["date", "id"], name: "BY_DATE", using: :btree
add_index "acct_transactions", ["transaction_type_id"], name: "fk_acct_transactions_transaction_types1_idx", using: :btree
end
def self.down
drop_table :acct_transactions
end
end