[Dbix-class] Lack of DWIMery

Nilson Santos Figueiredo Junior acid06 at gmail.com
Sat Apr 8 05:22:15 CEST 2006


Is there a specific reason for this not to work:

<code>
package OldModel::Foo;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('foo');
__PACKAGE__->add_columns(qw/id foo bar baz_id/);
__PACKAGE__->set_primary_key(qw/id/);
__PACKAGE__->belongs_to('baz', 'OldModel::Foo', 'baz_id');

package OldModel::Baz;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('baz');
__PACKAGE__->add_columns(qw/id special/);
__PACKAGE__->set_primary_key(qw/id/);

package OldModel;
use base 'DBIx::Class::Schema';
__PACKAGE__->register_class('Foo', 'OldModel::Foo');
__PACKAGE__->register_class('Baz', 'OldModel::Baz');

package main;
my $schema = OldModel->connect(...);

my $new_baz = $schema->resultset('Baz')->create({special => 'weee'});
my $new_foo = $schema->resultset('Foo')->create({foo => 'quux', bar =>
'woz', baz => $new_baz});

Result: DBIx::Class::ResultSet::create(): No such column baz at test.pl line 88
</code>

Now, I know that if I change 'baz' to 'baz_id' it will work. But why
can't it just DWIM and infer the column from the relationship? Is
there something fundamental I'm missing here?

Thinking about this makes me wonder if relationships shouldn't have
more magic attached to them so that, in this case, $foo->baz_id would
really be a sort of alias to $foo->baz->id and could even be used
exchangeably. Of course I'm aware this might not be so simple for more
complex relationships, but as long as you can map one table to
another, it should be possible to infer these relationships.

But of course, there could be something completely obvious that I'm
missing. If so, please point it out. Otherwise... is this a desirable
feature?

Just FYI, this is holding me back from making the Schema::Declare
stuff to be as transparent as mst thinks it should be (i.e. abstract
FKs away from the user). By the way,  this new stuff is already
somewhat functioning (if you follow its not yet documented conventions
(which will be overridable)).

-Nilson Santos F. Jr.



More information about the Dbix-class mailing list