我在postgresql 9.3上运行Rails 4,我实现了以下多对多关系
第一个模型:
class Cms::ContentEntry < ActiveRecord::Base
self.table_name = 'cms_content_entries'
has_many :entries_media, class_name: 'Join::EntryMedia', foreign_key: :cms_content_entry_id
has_many :media_items, through: :entries_media, class_name: 'Media::Item', source: :media_item
end
加入模型:
class Join::EntryMedia < ActiveRecord::Base
self.table_name = 'join_entries_media'
belongs_to :entry, class_name: 'Cms::ContentEntry'
belongs_to :media_item, class_name: 'Media::Item'
end
第二种模式:
class Media::Item < ActiveRecord::Base
self.table_name = 'media_items'
has_many :entries_media, class_name: 'Join::EntryMedia', foreign_key: :media_item_id
has_many :entries, through: :entries_media, class_name: 'Cms::ContentEntry', source: :entry
end
但是在脚本控制台中,当我运行时:
Cms::ContentEntry.last.media_items
我收到错误:
SELECT "media_items".* FROM "media_items" INNER JOIN "join_entries_media" ON
"media_items"."id" = "join_entries_media"."media_item_id" WHERE
"join_entries_media"."cms_content_entry_id" = $1 [["cms_content_entry_id", 2]]
PG::UndefinedFunction: ERROR: operator does not exist: uuid = integer
LINE 1: ...R JOIN "join_entries_media" ON "media_items"."id" = "join_en...
^
HINT: No operator matches the given name and argument type(s). You might need to
add explicit type casts.: SELECT "media_items".* FROM "media_items" INNER JOIN
"join_entries_media" ON "media_items"."id" = "join_entries_media"."media_item_id"
WHERE "join_entries_media"."cms_content_entry_id" = $1
以下是我的架构的样子:
CREATE TABLE cms_content_entries (
id integer NOT NULL,
author_id integer,
title character varying(255),
body text,
metadata hstore,
published_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
CREATE TABLE join_entries_media (
id integer NOT NULL,
cms_content_entry_id integer,
media_item_id integer
);
CREATE TABLE media_items (
id uuid DEFAULT uuid_generate_v4() NOT NULL,
element_id bigint,
element_type character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone,
user_id integer
);
答案 0 :(得分:0)
我刚创建了一个迁移,将数据类型更改为uuid:
def change
create_table :join_entries_media do |t|
t.integer :cms_content_entry_id
t.column :media_item_id, :uuid
end