Rails 4 MySQL bigInt 主键问题和错误
Rails 4 MySQL bigInt primary key issues and errors
我需要使用 14 位 bigInt 作为 rails 4.1.8 应用程序中的主键。使用 SO 上的旧帖子作为指南,我想出了以下解决这个问题的方法...
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"赋值为主键,它只是另一个普通的字段。另外,当我收到以下错误时...
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)
当我尝试运行以下种子文件时...
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
迁移 运行 没问题,但 table 不会播种并且 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
请注意第 12 行的 execute 语句。当我在那里时,我还将 "date" 字段更改为时间戳,无论如何我本来打算这样做.
它不漂亮并且违反了 "convention" 但它工作得很好,所以我可以继续前进。感谢观看。
我需要使用 14 位 bigInt 作为 rails 4.1.8 应用程序中的主键。使用 SO 上的旧帖子作为指南,我想出了以下解决这个问题的方法...
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"赋值为主键,它只是另一个普通的字段。另外,当我收到以下错误时...
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)
当我尝试运行以下种子文件时...
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
迁移 运行 没问题,但 table 不会播种并且 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
请注意第 12 行的 execute 语句。当我在那里时,我还将 "date" 字段更改为时间戳,无论如何我本来打算这样做. 它不漂亮并且违反了 "convention" 但它工作得很好,所以我可以继续前进。感谢观看。