|X-Mailer:||MIME-tools 5.418 (Entity 5.418)|
|Subject:||Problems in setting up relationships with loaded classes|
There is a subtle bug using Class::DBI::Loader and many-to-many relationships. The attached tarball contains a test case. To excercise the bug, run 'make.pl' followed by 'test.pl'. 'make.pl' will create a SQLite DB with 3 tables: ai <->> ci <<-> bi 'ci' is the middle table for the many-to-many relation. It will then insert 1 record in each table. 'test.pl' will load the classes with CDBI::Loader, 'require' them to set up relationships, then try to access the 'bi' record associated with the 'ai' record. The error is 'ai is not a column of Bi'. This means that Ai->has_many did not execute correctly. What happens is as follows: - CDBI::Loader creates the three classes, setting up their inheritance tree - Ai calls 'has_many' - CDBI::R::HasMany::remap_arguments calls '_require_class('Ci')' - CDBI::_require_class sees that Ci::ISA exists, and does not 'require Ci' - remap_arguments lacks data to divine the relation columns, and gives wrong results If Ci is required *before* Ai and Bi, everything works, because 'has_a' does not need information extracted from the foreign class. I don't have a way to fix this issue, since it is not really possible to know: - whether the class has been created by CDBI::Loader or by executing the "proper" file - whether relationships will be set up by the file or the loader - what is the right file to require (since it's possible to define several classes in a single source file) The best I can think of is a documentation patch, saying that if someone sets up relationships manually in thei class files, they have to be 'require'd in the correct order after the call to Loader, but before using them. How did I discover it? Catalyst::Model::CDBI requires, in order, the base class that creates the Loader, then the table classes, in alphabetical order. Since my tables are called like 'stuff', 'stuff_thing', 'thing', the relations don't work automatically.
Message body not shown because it is not plain text.