mysql - Ruby on Rails 不正确的主键名称

标签 mysql ruby aptana radrails

我正在使用 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

这是你需要的:

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table

: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/

相关文章:

Mysql计算包含文本的同一字段中时间戳之间的时间差?

mysql - 具有多列的自定义订单

mysql - MySQL Select SQL 背后的操作

ruby - 你能在 Ruby 中的一行中创建/写入/附加一个字符串到文件吗

ruby-on-rails - Rails 低级缓存 : Update cache when ActiveRecord object updated_at changes OR when a new object is added to collection

aptana - eclim 可以与 aptana 集成吗?

php - 将选择表单的值插入mysql

python - 在 Aptana 中配置 Python 以将输出推送到控制台窗口

java - 为什么我在 Ubuntu 20.04 上从 Aptana 收到运行错误

ruby - 在包含任意数量的嵌套哈希和数组的哈希中查找键/值对