发布于 2011-04-07 00:47:38
开发这类代码的问题是,许多包含多个引用的表并不是多对多表,并且由于其他原因而具有多个引用。例如,我将为一些虚构的应用程序构建一个模式,其中某些东西可以被视为多对多表,但实际上并非如此。
create table category (
id primary key,
...
);
create table sub_category (
id primary key,
category references category(id),
...
);
/* EDIT:
This is the table that could be regarded as many_to_many
by an automated system */
create table product (
id primary key,
category references category(id),
sub_category references sub_category(id),
...
);为了便于使用,可以这样构建一些东西,而不必在网站上的数据库中进行多个表连接,特别是在考虑速度时。对于一段代码来说,很难明确地说出“这不是一个many_to_many”的情况,而开发人员应该能够轻松地找出它,并在校验和下面添加many_to_many行。
我认为DBIX::Class模式输出是一个很好的起点,仅此而已,尤其是在处理非MySQL数据库中的自动编号时。我经常需要修改上面的“不要在这一行上修改”之类的东西(当然,many_to_many显然可以低于这个校验和。
发布于 2011-04-02 09:27:10
这确实是一个有些根本性的问题-- many_to_many是“关系桥梁”,而不是“关系”。文档解释了"the difference between a bridge and a relationship is, that the bridge cannot be used to join tables in a search, instead its component relationships must be used."
另一方面,这意味着如果正确地发现了真实的关系,那么应该可以直接自动添加多对多关系:首先,搜索具有两个或更多has_many关系的表。然后,对于每一对这样的关系,创建一个多对多关系桥梁。(当然,人们可能希望DBIx::Class自己能做到这一点。)
https://stackoverflow.com/questions/5488261
复制相似问题