简体   繁体   English

为什么DBIx :: Class不能创建多对多访问器?

[英]Why does DBIx::Class not create many-to-many accessors?

While creating a schema from a database many-to-many relationships between tables are not created. 从数据库创建模式时,不会创建表之间的多对多关系。

Is this a principal problem? 这是一个主要问题吗?

Is it possible to detect from the table structure that many-to-many relationships exist and create the respective code in schema classes automagically? 是否有可能从表结构中检测到存在多对多关系并自动在模式类中创建相应的代码?

It is indeed a somewhat fundamental problem -- many_to_many is a " relationship bridge " and not a " relation ." 这确实是一个基本问题 - many_to_many是一个“ 关系桥梁 ”而不是“ 关系” The documentation explains that "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." 该文档解释了“桥和关系之间的区别在于,桥不能用于在搜索中连接表,而是必须使用它的组件关系。”

On the other hand, this means that if the real relationships are correctly discovered it should be straightforward to add the many-to-many relationships automatically: First, search for tables that have two or more has_many relationships. 另一方面,这意味着如果正确发现了真实关系,则应该直接自动添加多对多关系:首先,搜索具有两个或更多has_many关系的表。 Then, for each pair of such relationships, create a many-to-many relationship bridge. 然后,对于每对这样的关系,创建一个多对多关系桥。 (Of course, one might hope that DBIx::Class would do this itself.) (当然,人们可能希望DBIx :: Class本身就能做到这一点。)

The problem with developing this kind of code is that many tables that contain multiple references are not many-to-many tables, and have multiple references for other reasons. 开发此类代码的问题在于,包含多个引用的许多表不是多对多表,并且由于其他原因而具有多个引用。 For instance, I'll make up a schema for some fictional app where something could be regarded as a many-to-many table, when it is not. 例如,我将构建一个虚构应用程序的架构,其中某些东西可以被视为多对多表,而不是。

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),
    ...
);

Something could be built this way for ease of use, without having to do multiple table joins in the database on a website, especially when considering speed. 可以通过这种方式构建一些易于使用的东西,而无需在网站上的数据库中进行多个表连接,尤其是在考虑速度时。 It would be difficult for a piece of code to say definitively 'this is not a many_to_many' situation, while the developer should be able to easily figure it out, and add in the many_to_many line below the checksum. 一段代码很难明确地说'这不是一个多少'的情况,而开发人员应该能够轻松搞清楚,并添加校验和下面的many_to_many行。

I consider DBIX::Class schema outputs a good starting point, and little more, especially when working with auto numbering in non-MySQL databases, among other things. 我认为DBIX :: Class模式输出一个很好的起点,而且更多,尤其是在非MySQL数据库中使用自动编号时。 I often need to modify above the "Don't modify above this line" stuff (although many_to_many can obviously go below that checksum, of course. 我经常需要修改上面的“不要在这行之上修改”的东西(当然,many_to_many显然可以低于那个校验和)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM