我尝试配置Texticle并在迁移时遇到此错误。错误消息指的是我用<<<<<<
标记的行PGError: ERROR: schema "half_links" does not exist
: CREATE VIEW searches AS
SELECT items.id AS searchable_id, items.name AS term,
CAST('Item' AS varchar) AS searchable_type
FROM items
UNION
SELECT half_links.id AS searchable_id, half_links.item.name AS term, <<<<
CAST('HalfLink' AS varchar) AS searchable_type
FROM half_links
这是我的迁移
class CreateSearches < ActiveRecord::Migration
def self.up
ActiveRecord::Base.connection.execute <<-SQL
CREATE VIEW searches AS
SELECT items.id AS searchable_id, items.name AS term,
CAST('Item' AS varchar) AS searchable_type
FROM items
UNION
SELECT half_links.id AS searchable_id, half_links.item.name AS term,
CAST('HalfLink' AS varchar) AS searchable_type
FROM half_links
SQL
end
和模型
:half_link belongs_to :item
:item has_many :half_links
我在我的localhost PSQL上运行它。我怎么能绕过这个?
答案 0 :(得分:1)
错误在第二个选择中。 half_links.item.name
使Postgres假设half_links是模式名称(正确的语法是..)
看到你的关联是怎样的,我想你的意思是得到属于half_links的项目的名称。
由于Postgres不了解Rails协会的工作原理,您需要自己获取数据:
CREATE VIEW searches AS
SELECT items.id AS searchable_id, items.name AS term,
CAST('Item' AS varchar) AS searchable_type
FROM items
UNION
SELECT half_links.id AS searchable_id, items.name AS term,
CAST('HalfLink' AS varchar) AS searchable_type
FROM half_links join items on half_links.item_id = items.id
这假设half_links
到items
中的外键被称为item_id
,如果您使用Rails迁移创建表,它应该是。{/ p>
此外,这只会显示实际上有与之关联的项目的half_links,如果可能有没有项目的那些,则将join
更改为outer join
。在这些情况下,术语将为NULL。