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
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: 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
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?
Much thanks