[Dbix-class] create() has_one problems

Carl Franks fireartist at gmail.com
Mon Jan 14 11:40:58 GMT 2008


I'm unable to use create() to create a new record with a has_one relationship.
The docs (ResultSet/create) say: for has_one rels, "pass an arrayref
of hashrefs".

If I pass an arrayref containing a hashref, like so:

$rs->create({
    text_col => 'filler2',
    user => [ { name => 'foo' } ]
} );

I get this error message - I don't know where the "NAME" in the SQL comes from:

SELECT me.id, me.master, me.name FROM user me WHERE ( ( ( ( me.id NAME
? ) ) ) ): 'foo'
DBI Exception: DBD::SQLite::db prepare_cached failed: near "NAME":
syntax error(1) at dbdimp.c line 271 [for Statement "SELECT me.id,
me.master, me.name FROM user me WHERE ( ( ( ( me.id NAME ? ) ) ) )"]
at /opt/perl-5.8.8/lib/site_perl/5.8.8/DBIx/Class/Schema.pm line 945

If I get rid of the arrayref, and just pass a hashref for the rel data, like so:

$rs->create({
    text_col => 'filler2',
    user => { name => 'foo' }
} );

The inserts work, but the related (user) table doesn't get the
belongs_to id set.
Setting DBIC_TRACE shows the SQL being generated:

SELECT me.id, me.master, me.name FROM user me WHERE ( me.name = ? ): 'foo'
BEGIN WORK
INSERT INTO master (id, text_col) VALUES (?, ?): 'NULL', 'filler2'
SELECT COUNT( * ) FROM user me WHERE ( name = ? ): 'foo'
INSERT INTO user (name) VALUES (?): 'foo'
COMMIT

I don't know why it's trying to retrieve a has_one rel (user) before
the main entry (master) is even inserted.
The "SELECT COUNT(*)" also looks suspicious - is that intended?

Am I using this wrong?

I'm using DBIx::Class 0.08008, DBD::SQLite 1.14, sqlite 3.4.2-3.
Copied below is the 2 relevant table classes (with irrelevant cols and
rels deleted for clarity).

package MySchema::Master;
use base 'DBIx::Class';

__PACKAGE__->load_components(qw/
    InflateColumn::DateTime Core
/);

__PACKAGE__->table("master");

__PACKAGE__->add_columns(
    id             => { data_type => "INTEGER" },
    text_col       => { data_type => "TEXT" },
);

__PACKAGE__->set_primary_key("id");

__PACKAGE__->has_one( user => 'MySchema::User', 'master' );

1;

package MySchema::User;
use base 'DBIx::Class';

__PACKAGE__->load_components(qw/ Core /);

__PACKAGE__->table("user");

__PACKAGE__->add_columns(
    id     => { data_type => "INTEGER" },
    master => { data_type => "INTEGER" },
    name   => { data_type => "TEXT" },
);

__PACKAGE__->set_primary_key("id");

__PACKAGE__->belongs_to( master => 'MySchema::Master', 'id' );

1;



More information about the DBIx-Class mailing list