Rails 3 夹具连接表 ID 不正确

发布于 2024-10-07 14:35:05 字数 1134 浏览 0 评论 0原文

我正在尝试使用导轨中的固定装置。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-10-14 14:35:05

我假设您已经定义了正确的关联。您只需要对您的灯具进行一些小的更改:

**tags.yml**
one:
  tag: pie
  posts: two

two:
  tag: cake
  posts: one, two

**posts.yml**
one:
  subject: subject
  content: content
  slug: woot
  tags: 
    - two

two:
  subject: subject2
  content: content2
  slug: this_is_good
  tags: 
    - one
    - two

编辑:我从灯具中删除了所有 ID。这会生成正确的引用。但这并不是完全正确的答案。

I am assuming you have proper associations defined. You just need a small change in your fixtures:

**tags.yml**
one:
  tag: pie
  posts: two

two:
  tag: cake
  posts: one, two

**posts.yml**
one:
  subject: subject
  content: content
  slug: woot
  tags: 
    - two

two:
  subject: subject2
  content: content2
  slug: this_is_good
  tags: 
    - one
    - two

EDIT: I deleted all the IDs from fixtures. That generates correct references. But this isn't quite the correct answer.

冰之心 2024-10-14 14:35:05

我假设你的模型是这样的。

class Tag Post < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

class Post < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

这意味着 posts_tags 连接表只能将 post_id 和 tag_id 作为列。它不能有 rowid 列。您需要类似 :id =>; posts_tags 的表定义中为 false

I'm gonna assume your models are something like this.

class Tag Post < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

class Post < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

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.

初吻给了烟 2024-10-14 14:35:05

也许您的测试数据库有一个旧模式,您在其中使用了“rowid”列?

尝试:

rake db:test:clone

Maybe your test database has an old schema where you used a "rowid" column?

Try:

rake db:test:clone
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文