【发布时间】:2016-11-20 01:14:15
【问题描述】:
我有两个表名为 Post 和 Like。我想在 Like 表中设置“ip”和“reference to post”作为我的两个主键。我可以从帖子表中成功创建具有两个唯一键“ip”和“id”的喜欢表。但是当我尝试从我的 rails 控制台创建示例数据时,它会首先检查是否存在相同的“ip”。如果相同的“ip”已经存在,即使帖子完全不同,它也不会创建数据。
这是我目前所拥有的。
喜欢表
class CreateLikes < ActiveRecord::Migration def change create_table :likes, :id => false do |t| t.string :ip, limit: 39 t.references :post, index: true, foreign_key: true t.timestamps null: false end add_index :likes, ["ip"], :unique => true add_index :posts, ["id"], :unique => true end结束
帖子表
class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.string :body t.string :media t.timestamps null: false end end end
【问题讨论】:
-
请注意
t.references :post添加了一个名为post_id的整型列,而不是post。
标签: mysql ruby-on-rails activerecord