Rails Postgresql 多个模式和相同的表名
我有两个不同模式的表,例如 案例
和事件
。
在每个模式中,我都有表 basic
events.basic
cases.basic
该表具有关系:
events.basic
有一个cases.basic
(cases.basic
有许多events.basic
)
我的尝试失败了:
文件 cases_basic.rb
class CasesBasic < ActiveRecord::Base
set_table_name 'cases.basic'
set_primary_key 'case_id'
has_many :Events, :class_name => 'EventsBasic', :foreign_key => 'case_id'
end
文件events_basic.rb
class EventsBasic < ActiveRecord::Base
set_table_name 'events.basic'
set_primary_key 'event_id'
belongs_to :Case, :class_name => 'CasesBasic', :foreign_key => 'case_id'
end
环境: Ruby 1.9.3
、Rails 3.1.3
、gem 'pg'
我需要这个问题的答案:
- 如何在 Rails Active 中处理这种情况记录?
- 如何查询这个表?
- 如何在
rake db:schema:dump
中处理这种情况
编辑:
更改belongs_to
和has_many
(如Catcall建议)后我有同样的错误
PGError: ERROR: column basic.case_id does not exist
LINE 1: ...IN "cases"."basic" ON "cases"."basic"."case_id" = "events"."...
^
: SELECT "events"."basic".* FROM "events"."basic" INNER JOIN "cases"."basic" ON "cases"."basic"."case_id" = "events"."basic"."case_id" LIMIT 3
Rails 生成错误的 SQL。我应该使用一些别名来完成:
SELECT t1.* FROM "events"."basic" t1 INNER JOIN "cases"."basic" t2 ON t1."case_id" = t2."case_id" LIMIT 3
EDIT 2: 好吧,这是我的错误,我没有在示例数据库中添加 events.basic.case_id 列和外键。 它有效!
问题 1 和 2 有效,但我们对 rake db:schema:dump
有疑问,那又如何呢? Rails 仅为公共模式生成模型。
我有很多表和关系,我想生成它们。
I have two tables in two different schemas e.g.cases
and events
.
In each schema I have table basic
events.basic
cases.basic
This tables have relations:
events.basic
has onecases.basic
(cases.basic
has manyevents.basic
)
My attempts have failed:
file cases_basic.rb
class CasesBasic < ActiveRecord::Base
set_table_name 'cases.basic'
set_primary_key 'case_id'
has_many :Events, :class_name => 'EventsBasic', :foreign_key => 'case_id'
end
file events_basic.rb
class EventsBasic < ActiveRecord::Base
set_table_name 'events.basic'
set_primary_key 'event_id'
belongs_to :Case, :class_name => 'CasesBasic', :foreign_key => 'case_id'
end
Enviroment:
Ruby 1.9.3
, Rails 3.1.3
, gem 'pg'
I Need answer for this questions:
- how to handle this situation in Rails Active Record?
- how to query this tables?
- how to handle this situation in
rake db:schema:dump
EDIT:
After changing belongs_to
and has_many
(like Catcall suggest) i have the same error
PGError: ERROR: column basic.case_id does not exist
LINE 1: ...IN "cases"."basic" ON "cases"."basic"."case_id" = "events"."...
^
: SELECT "events"."basic".* FROM "events"."basic" INNER JOIN "cases"."basic" ON "cases"."basic"."case_id" = "events"."basic"."case_id" LIMIT 3
Rails generate bad SQL. I should be done using some aliases:
SELECT t1.* FROM "events"."basic" t1 INNER JOIN "cases"."basic" t2 ON t1."case_id" = t2."case_id" LIMIT 3
EDIT 2:
Ok It was my f*** bug, i didn't add events.basic.case_id column and foreign key in my example database. It works!
Questions 1 AND 2 are working but we have question about rake db:schema:dump
what about it? Rails generates models only for public schema.
I have so many tables and relations that i want to generate them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用 pg_power gem。它提供了在迁移中创建 PostgreSQL 模式的语法,如下所示:
并且还负责将模式正确转储到 schema.rb 文件中。
I would recommend using pg_power gem. It provides syntax for creating PostgreSQL schemas in migrations like this:
And also takes care about dumping schemas into schema.rb file correctly.
查看 http://blog。 jerodsanto.net/2011/07/building-multi-tenant-rails-apps-with-postgresql-schemas/
这描述了如何配置 Rails 应用程序以使用具有多个模式的 Postgres 数据库。他将表查找比作 Unix 路径的功能,从特定位置开始,然后回到一般位置。
集成架构路径后,您可以成功查询这些表。 db:schema:dump 将使用您的应用程序喜欢的相同模式优先级来读取表。
Check out http://blog.jerodsanto.net/2011/07/building-multi-tenant-rails-apps-with-postgresql-schemas/
This describes how to configure a Rails application to use a Postgres database with multiple schemas. He likens the table lookup to the functionality of the Unix path, starting with specific locations, and falling back to general locations.
Once your schema paths are integrated, you can query these tables successfully. db:schema:dump will read the tables using the same schema precedence that your application prefers.
[编辑:进一步阅读后,我认为 ActiveRecord 根本不能很好地支持多种模式。但我可能是错的。我暂时将这个答案留在这里,尽管它几乎肯定是错误的。 (概念上是对的。但是构建 ActiveRecord 的人可能没有与任何数据库人员交谈,因为数据库人员可能知道什么?)看起来 IBM 在 2008 年就在解决这个问题,但是我不明白这项工作是如何结束的。]
PostgreSQL 在设置对不同模式中具有相同名称的表的外键引用时没有任何问题。像这样的代码
可能需要进行模式限定。
现在,表cases.basic“有很多”事件是不正确的,是吗?不,它“有很多”事件。基本。在你的两堂课中进行这种改变,并让我们知道它是如何运作的。 (这里没有 Rails,否则我会为你测试一下。)
[Edit: after further reading, I don't think ActiveRecord supports multiple schemas well at all. But I could be wrong. I'll leave this answer here for the time being, although it's almost certainly wrong. (Conceptually it's right. But the people who built ActiveRecord probably didn't talk to any database people, because what could database people possible know?) It looks like IBM was working on this problem in 2008, but I don't see how that work ended.]
PostgreSQL doesn't have any trouble setting foreign key references to tables that have the same name in different schemas. Code like this
probably needs to be schema-qualified.
Now, it's not true that the table cases.basic "has many" events, is it? No, it "has many" events.basic. Carry that kind of change throughout your two classes, and let us know how that works. (No Rails here, or I'd test it for you.)