[Dbix-class] HasMany foreign column guess

Bill Moseley moseley at hank.org
Mon Apr 23 06:15:07 GMT 2007


If the foreign key is not specified DBIx::Class::Relationship::HasMany
makes this as a guess:

    $class =~ /([^\:]+)$/;
      $f_key = lc $1; # go ahead and guess; best we can do
      $guess = "using our class name '$class' as foreign key";
    }

Would a better guess be to look at the foreign table and see in any
columns are pointing to the table making the has_many relationship?
That is, look for a belongs_to relationship on the class.


It seems like it's often that tables are named, say, user_status and
then the table's class name is UserStatus.  I often name my foreign
keys the names of the tables, that is in the "user" table I might
have:

    user_status     integer NOT NULL REFERENCES user_status

So another guess might be simply:

    $f_key = $class->table;

Currently, it's guessing "userstatus".




-- 
Bill Moseley
moseley at hank.org




More information about the Dbix-class mailing list