class CreateBallots < ActiveRecord::Migration
def change
create_table :ballots do |t|
t.references :user
t.references :score
t.references :election
t.string :key
t.timestamps
end
add_index :ballots, :user
add_index :ballots, :score
add_index :ballots, :election
end
end
kết quả trong:Rails: "t.references" không hoạt động khi tạo index
SQLite3::SQLException: table ballots has no column named user: CREATE INDEX "index_ballots_on_user" ON "ballots" ("user")/home/muhd/awesomevote/db/migrate/20130624024349_create_ballots.rb:10:in `change'
Tôi nghĩ t.references
lẽ ra phải xử lý đó đối với tôi?
Ah, được rồi. Tôi đã nghĩ rằng cú pháp tương tự sẽ được sử dụng giữa t.references và add_index, nhưng tôi đoán đó không phải là trường hợp ở đây. Cảm ơn bạn đã thực hiện mẹo 'index: true'; Tôi không biết về điều đó. – Muhd
Có, nó không thực sự rõ ràng nhưng "t.references" tham chiếu đến "người dùng" bằng cách thêm cột "user_id" :) –