防止导出特定关系的外键
我正在寻找一种方法来防止学说导出特定关系的外键。例如:
Item:
connection: doctrine
#attributes:
# export: tables
columns:
store_id: integer(4)
shelf_id: integer(4)
relations:
Store:
local: store_id
foreign: id
foreignAlias: Items
Shelf:
local: shelf_id
foreign: id
foreignAlias: Items
Shelf:
connection: doctrine
columns:
name: string(255)
Store:
connection: store
columns:
name: string(255)
在这里,如果我构建此模式,学说将为项目表生成 2 个外键:
ALTER TABLE item ADD CONSTRAINT item_store_id_store_id FOREIGN KEY (store_id) REFERENCES store(id);
ALTER TABLE item ADD CONSTRAINT item_shelf_id_shelf_id FOREIGN KEY (shelf_id) REFERENCES shelf(id);
如果取消注释“属性”部分,学说将不会创建任何外键。我只需要 item_shelf_id_shelf_id
约束。我想要它的原因是因为 Item 和 Shelf 表位于同一个数据库中,而 Store 位于不同的数据库中 - 它的外键根本不适用。
I looking for a way to prevent doctrine from exporting the foreign keys for specific relations. For example:
Item:
connection: doctrine
#attributes:
# export: tables
columns:
store_id: integer(4)
shelf_id: integer(4)
relations:
Store:
local: store_id
foreign: id
foreignAlias: Items
Shelf:
local: shelf_id
foreign: id
foreignAlias: Items
Shelf:
connection: doctrine
columns:
name: string(255)
Store:
connection: store
columns:
name: string(255)
Here, if I build this schema, doctrine would generate 2 foreign keys for Item table:
ALTER TABLE item ADD CONSTRAINT item_store_id_store_id FOREIGN KEY (store_id) REFERENCES store(id);
ALTER TABLE item ADD CONSTRAINT item_shelf_id_shelf_id FOREIGN KEY (shelf_id) REFERENCES shelf(id);
If you uncomment 'attributes' section, doctrine won't create any. Where as I only need item_shelf_id_shelf_id
constraint. The reason I want it is because Item and Shelf tables are in the same database, and Store is in different database - the foreign key for it simply won't apply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用 MySQL,此线程表示数据库之间支持外键: http://forums.mysql.com/read.php?22,150829,200251#msg-200251 所以问题可能是生成的 sql 不包含数据库名称。
SQL 是在
Doctrine_Export::getForeignKeyBaseDeclaration
中构建的,其中包含以下行:因此,如果数据库是,则在 schema.yml 中添加包含数据库名称的
foreignTable
键可能会有所帮助。在同一台服务器上。Assuming you are using MySQL, this thread says foreign keys are supported between databases : http://forums.mysql.com/read.php?22,150829,200251#msg-200251 So the problem might rather be that the generated sql does not contain the db name.
The SQL is build in
Doctrine_Export::getForeignKeyBaseDeclaration
, which contains these lines:So adding a
foreignTable
key containing your database name in your schema.yml might help, if the database is on the same server.