[Dbix-class] SQL::Translator::Parser::DBIx::Class

Matt S Trout dbix-class at trout.me.uk
Mon Mar 27 20:05:31 CEST 2006


Stephan Szabo wrote:
> On Mon, 27 Mar 2006, Matt S Trout wrote:
> 
>> Stephan Szabo wrote:
>>> On Sun, 26 Mar 2006, Stephan Szabo wrote:
>>>
>>>> While playing with SQL::Translator to generate sql scripts from schemas, I
>>>> noticed that the DBIx::Class parser seemed to grab only one column of
>>>> multi-column relationships. It looks like just making it work across
>>>> $rel_info->{cond} into an array rather than taking only the first key
>>>> should work, something like:
>>>>
>>>> my @cond = keys(%{$rel_info->{cond}});
>>>> my @keys = map {/^\w+\.(\w+)$/} @cond;
>>>> my @refkeys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
>>> And I should really actually cut and paste rather than doing it freehand
>>> when I copy something into an email and/or check it before sending. The
>>> above assignments are backwards (it should be refkeys in the first and
>>> keys in the second).
>>>
>>>> if($rel_table)
>>>> {
>>>>     $table->add_constraint(
>>>>                 type             => 'foreign_key',
>>>>                 name             => "fk_$keys[0]",
>>>>                 fields           => \@keys,
>>>>                 reference_fields => \@refkeys,
>>>>                 reference_table  => $rel_table,
>>>>     );
>>>> }
>>>>
>>>> Does that seem reasonable?
>> Seems like a damn good start to me. If you grab svn and apply that tweak then
>> get maint/gen-schema.pl to load the HelperRels stuff so it's got some
>> relationships to work with and see what the SQLite file you end up with looks
>> like?
> 
> Sadly, it looks like at least the version of the SQLite producer I have
> ignores foreign key constraints (as even the existing relationships don't
> seem to produce single column foreign keys). I'm working on seeing how
> hard it'd be to try it against a different backend to see what gets
> produced.

Err, if you're using an untouched maint/gen-schema.pl I intentionally didn't 
load the relationships (for the tests they're defined in BasicRels.pm and 
HelperRels.pm which are loaded explicitly by the two different sets of tests) 
- so until you add loading one of them you ain't going to get FK constraints 
produced on any DB.

> No problem, it was just the first time I'd played with innards of this
> stuff, so I thought I should make sure I wasn't chasing geese.

Nope, definitely looks like the right direction. Add something to make sure it 
only FKs when it should be doing (based on join type and other bits as noted 
in that comment) and you should be golden.

Please do wander onto irc.perl.org#dbix-class if you want to bug people 
directly; I'm usually on there early afternoon until 3-4am  (GMT).

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list