Rails 4 MySQL bigInt主键问题和错误 [英] Rails 4 MySQL bigInt primary key issues and errors
问题描述
在Rails 4.1.8应用程序中,我需要使用14位的 bigInt 作为主键.以SO上较旧的帖子为指导,我提出了以下解决方案...
I need to use a 14-digit bigInt as a primary key in a rails 4.1.8 application. Using older posts on SO as a guide, I came up with the following to address this...
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"分配为主键,它只是另一个普通字段.另外,当我收到以下错误消息时...
However, this method doesn't really assign "id" as a primary key, it is just another ordinary field. Also, when I get the following error ...
Mysql2 :: Error:字段'id'没有默认值:INSERT INTO
acct_transactions
(account_id
,amount
,date
,description
,transaction_type_id
)值(224149525446,222.450361056561, '1970-12-18 00:00:00','Transfer',6)
Mysql2::Error: Field 'id' doesn't have a default value: INSERT INTO
acct_transactions
(account_id
,amount
,date
,description
,transaction_type_id
) VALUES (224149525446, 222.450361056561, '1970-12-18 00:00:00', 'Transfer', 6)
当我尝试运行以下种子文件时...
when I try to run the following seed file...
account_transactions = []
accounts.each do |i|
80.times do |j|
type = types.sample
case (type)
...
end
t = AcctTransaction.new
t.id = SecureRandom.random_number(99999999999999) # 14-digit BigInt
t.account_id = accounts[j].id
t.transaction_type_id = type
t.date = Time.at((Time.now.month - 18) + rand * (Time.now.to_f)).to_date
t.description = description
t.amount = amount
t.save
account_transactions << t
end
end
迁移运行正常,但是表不会播种并且id不是主要的.我做错了吗?还是有更好的方法来做到这一点?
The migration runs fine, but the table won't seed and id is not primary. Have I made an error? Or is there a better way to do this?
非常感谢
推荐答案
我通过使用SQL执行编写迁移来修复它,如下所示:
I fixed it by writing the migration with SQL execution like so:
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
请注意第12行的 execute 语句.当我在那儿时,我还将"date"字段更改为时间戳,无论如何我原本打算这样做. 它不是很漂亮,并且违反了常规",但是它工作得很好,所以我可以继续.感谢您的关注.
Note the execute statement @ line 12. While I was in there, I also changed the "date" field to a timestamp, which I meant to do originally anyway. It's not pretty and violates "convention" but it works perfectly, so I can move on. Thanks for looking.
这篇关于Rails 4 MySQL bigInt主键问题和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!