我正在使用 RadRails 创建我的 MYSQL 数据库表。这是迁移任务:
class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.string :eventname
t.string :evententryurl
t.string :eventurl
t.timestamps
end
end
def self.down
drop_table :events
end
end
然后我运行一个 db:migrate Rake 任务。这将在数据库表中创建以下字段:
id
eventname
evententryurl
eventurl
好的,到此为止。我遇到的问题是当我运行应用程序并转到 http://localhost/events/new 时该应用程序正在寻找 eventid,因为它是主键,而不是 id。如何更改我的迁移任务,使其自动将 eventid 作为主键放入数据库?
最佳答案
听起来您正试图在迁移中使用 eventid
覆盖 id
的默认主键名称。我假设应用程序正在寻找 eventid
而不是 id
因为您在模型中使用了 set_primary_key :eventid
。
这是你需要的:
:primary_key The name of the primary key, if one is to be added automatically. Defaults to id. If :id is false this option is ignored.
Also note that this just sets the primary key in the table. You additionally need to configure the primary key in the model via the set_primary_key macro. Models do NOT auto-detect the primary key from their table definition.
关于mysql - Ruby on Rails 不正确的主键名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3952883/