Rails 3 夹具连接表 ID 不正确
我正在尝试使用导轨中的固定装置。 rake db:fixtures:load 运行没有错误,并且表填充了我的数据,但我的多对多联接表包含不正确的 ID:
示例:
**tags.yml**
one:
id: 1
tag: pie
posts: two
two:
id: 2
tag: cake
posts: one, two
**posts.yml**
one:
id: 1
subject: subject
content: content
slug: woot
tags: cake
two:
id: 2
subject: subject2
content: content2
slug: this_is_good
tags: pie, cake
运行迁移和 db:fixtures:load 后,我的表被填充如:
标签:
id------标签
1 ......馅饼
2 ......蛋糕
posts_tags:
rowid - post_id - tag_id
1 ...... 1 ...... 974324064
2 ...... 2 ...... 576189563
3 ...... 3 ...... 974324064
由于某种原因,正在创建自动生成的 tag_id 并将其填充到我的连接表中。为什么?
编辑
迁移定义为:
def self.up
create_table :posts do |t|
t.string :subject
t.text :content
t.timestamps
end
end
和
def self.up
create_table :tags do |t|
t.string :tag
t.timestamps
end
create_table :posts_tags, :id => false do |t|
t.integer :post_id
t.integer :tag_id
end
end
I am trying to work with fixtures in rails. The rake db:fixtures:load runs without error, and table populates my data, but my many to many join table contains incorrect ids:
Example:
**tags.yml**
one:
id: 1
tag: pie
posts: two
two:
id: 2
tag: cake
posts: one, two
**posts.yml**
one:
id: 1
subject: subject
content: content
slug: woot
tags: cake
two:
id: 2
subject: subject2
content: content2
slug: this_is_good
tags: pie, cake
After running migrations and db:fixtures:load
my tables get populated as:
tags:
id------tag
1 ...... pie
2 ...... cake
posts_tags:
rowid - post_id - tag_id
1 ...... 1 ...... 974324064
2 ...... 2 ...... 576189563
3 ...... 3 ...... 974324064
For some reason an auto-generated tag_id is getting created and being populated in my join table. Why?
Edit
The migrations are defined as:
def self.up
create_table :posts do |t|
t.string :subject
t.text :content
t.timestamps
end
end
and
def self.up
create_table :tags do |t|
t.string :tag
t.timestamps
end
create_table :posts_tags, :id => false do |t|
t.integer :post_id
t.integer :tag_id
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您已经定义了正确的关联。您只需要对您的灯具进行一些小的更改:
编辑:我从灯具中删除了所有 ID。这会生成正确的引用。但这并不是完全正确的答案。
I am assuming you have proper associations defined. You just need a small change in your fixtures:
EDIT: I deleted all the IDs from fixtures. That generates correct references. But this isn't quite the correct answer.
我假设你的模型是这样的。
这意味着 posts_tags 连接表只能将 post_id 和 tag_id 作为列。它不能有 rowid 列。您需要类似
:id =>; posts_tags 的表定义中为 false
。I'm gonna assume your models are something like this.
That means the the posts_tags join table can only have post_id and tag_id in it as columns. It cannot have a rowid column. You'll need something like
:id => false
in the table definition of posts_tags.也许您的测试数据库有一个旧模式,您在其中使用了“rowid”列?
尝试:
Maybe your test database has an old schema where you used a "rowid" column?
Try: