postgresql Rails 4 迁移:has_and_belongs_to_many 表名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21549133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:17:37  来源:igfitidea点击:

Rails 4 Migration: has_and_belongs_to_many table name

ruby-on-railsrubypostgresqlruby-on-rails-4

提问by patrickS

I currently try to switch a rails 3.2 app to rails 4.0. But I have one problem with a has_and_belongs_manymodel.

我目前尝试将 rails 3.2 应用程序切换到 rails 4.0。但是我有一个has_and_belongs_many模型问题。

I have created a test app and I have the same problem there. This is what I have done:

我创建了一个测试应用程序,我在那里遇到了同样的问题。这就是我所做的:

Created two models: foo_clip and foo_url

创建了两个模型:foo_clip 和 foo_url

class FooClip < ActiveRecord::Base
  has_and_belongs_to_many :foo_urls

  attr_accessible :id, :name
end


class FooUrl < ActiveRecord::Base
  has_and_belongs_to_many :foo_clips

  attr_accessible :url
end

After this I have updated the migration files:

在此之后,我更新了迁移文件:

class CreateFooClips < ActiveRecord::Migration
  def change
    create_table :foo_clips do |t|
      t.string :name
      t.timestamps
    end
  end
end

class CreateFooUrls < ActiveRecord::Migration
  def change
    create_table :foo_urls do |t|
      t.string :url
      t.timestamps
    end
  end
end

Now I have created the migration file for the has_and_belongs_to_many table

现在我已经为 has_and_belongs_to_many 表创建了迁移文件

class CreateFooClipsFooUrls < ActiveRecord::Migration
  def change
    create_table :foo_clips_foo_urls do |t|
      t.belongs_to :foo_url
      t.belongs_to :foo_clip
    end
  end
end

As last step I created a seed file for testing:

作为最后一步,我创建了一个用于测试的种子文件:

foourl1 = FooUrl.create!(:url => 'http://www.google.com')
foourl2 = FooUrl.create!(:url => 'http://www.apple.com')

fooclip1 = FooClip.create!(:name => 'TestClip1')

fooclip1.foo_urls << foourl1
fooclip1.foo_urls << foourl2

fooclip1.save

Now I did:

现在我做了:

rake db:drop
rake db:create
rake db:migrate
rake db:seed

And got this error:

并得到这个错误:

PG::UndefinedTable: ERROR:  relation "foo_clips_urls" does not exist
LINE 5:         WHERE a.attrelid = '"foo_clips_urls"'::regcla...
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
               pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"foo_clips_urls"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

If I take a look at the postgres database the table is called: foo_clips_foo_urls

如果我查看 postgres 数据库,该表被称为:foo_clips_foo_urls

Any ideas why this happens?

任何想法为什么会发生这种情况?

回答by patrickS

I fixed the problem by adding the join_table name to every model like:

我通过将 join_table 名称添加到每个模型来解决这个问题,例如:

has_and_belongs_to_many :foo_urls,  :join_table => :foo_clips_foo_urls