One important remark in the documentation:

When setting up the relationship the foreign class's has_a() declarations are examined to discover which of its columns reference our class. (Note that because this happens at compile time, if the foreign class is defined in the same file, the class with the has_a() must be defined earlier than the class with the has_many(). If the classes are in different files, Class::DBI should usually be able to do the right things, as long as all classes inherit Class::DBI before 'use'ing any other classes.)

So this should work by reordering the declarations:

package MyDictionary::En_To_Sk MyDictionary::En_To_Sk->columns(Primary => qw/id/); MyDictionary::En_To_Sk->columns(Essential => qw/eng_id sk_id/); # Declare the relationships MyDictionary::En_To_Sk->has_a('eng_id' => "MyDictionary::Engword"); MyDictionary::En_To_Sk->has_a('sk_id' => "MyDictionary::Skword"); package MyDictionary::Engword MyDictionary::Engword->columns(Primary => qw/id/); MyDictionary::Engword->has_many("translations", 'MyDictionary::En_To_S +k', 'eng_id'); package MyDictionary::Skword MyDictionary::Skword->columns(Primary => qw/id/); MyDictionary::Skword->has_many("translations", 'MyDictionary::En_To_Sk +', 'sk_id');

I suppose you have defined the other columns. The construction "['MyDictionary::En_To_Sk' => 'engword']" in the 'has_many' function in the class 'MyDictionary::En_To_Sk' is only necessary when the values in 'eng_id' and 'sk_id' are not the primary keys used in the classes 'package MyDictionary::Engword' and 'package MyDictionary::Skword'.

Hopes this helps.

Martell


In reply to Re: Class DBI many to many by martell
in thread Class DBI many to many by zooey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.