English 中文(简体)
Postrgresql使用Rails 3查找错误的多对多表
原标题:Postrgresql looking for the wrong many to many table with Rails 3

**更新:我发现了这个:

活动记录要求中间联接表的名称由它联接的表串联而成,按字母顺序排列

Sigh... **

我有3个模型:图像,视频和关键字。图片和视频看起来基本上是一样的,我一直在努力为它们做同样的关键词。但是,在图像中添加关键字效果良好,而在视频中添加关键字则不然。

关键字.rb

has_and_belongs_to_many :cases
has_and_belongs_to_many :images
has_and_belongs_to_many :videos

视频.rb

has_and_belongs_to_many :keywords

image.rb

has_and_belongs_to_many :keywords

schema.rb

create_table "images_keywords", :id => false, :force => true do |t|
  t.integer  "image_id"
  t.integer  "keyword_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "videos_keywords", :id => false, :force => true do |t|
  t.integer  "video_id"
  t.integer  "keyword_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

这样可以:

images_controller.rb

@image.keywords << Keyword.new(:word => "test")

这个:

视频_控制器.rb

@video.keywords << Keyword.new(:word => "test")

出现以下错误:

ActiveRecord::StatementInvalid in VideosController#create

PGError: ERROR:  relation "keywords_videos" does not exist
LINE 4:              WHERE a.attrelid =  "keywords_videos" ::regclas...
                                    ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc,      a.attnotnull
          FROM pg_attribute a LEFT JOIN pg_attrdef d
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum
         WHERE a.attrelid =  "keywords_videos" ::regclass
           AND a.attnum > 0 AND NOT a.attisdropped
         ORDER BY a.attnum

当我检查控制台是否向图像添加关键字时,它会正确地查找“images_keywords”。为什么它会为视频查找“keywords_videos”???

两者有什么不同?

最佳回答

当使用<code>has_and_belongs_to_many</code>时,联接表的名称需要按字母顺序列出相关模型,即:<code>keywords_videos</code〕而不是<code>videos_keywords</code>。

问题回答

暂无回答




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签