[Dbix-class] Nested create with relationships
Kate Yoak
kate at yoak.com
Fri Apr 18 04:13:40 BST 2008
Hi everyone!
I am new here - a cdbi convert.
I am creating a record with its has_many relationship and, though
create succeeds, I wind up with data in the has_many table without
the foreign key.
Here is the data that goes into create:
$VAR1 = {
'features' => [
{
'feature_id' => undef,
'feature' => 'kdkdk',
'ord' => '1'
},
{
'feature_id' => undef,
'feature' => 'kdkdkdk',
'ord' => '2'
},
{
'feature_id' => undef,
'feature' => 'jjjj',
'ord' => '3'
}
],
'sku' => 'cccc',
'name' => 'kdkdk',
'description' => 'kdkdkdk'
};
The relationship's foreign key is product_id - it is created as a
result of the main insert - but is not propagated into the feature
table. Here are the inserts:
INSERT INTO product (description, name, sku) VALUES (?, ?, ?):
'kdkdkdk', 'kdkdk', 'cccc'
SELECT COUNT( * ) FROM product_feature me WHERE ( feature = ? AND
feature_id IS NULL AND ord = ? ): 'kdkdk', '1'
INSERT INTO product_feature (feature, feature_id, ord) VALUES
(?, ?, ?): 'kdkdk', 'NULL', '1'
SELECT COUNT( * ) FROM product_feature me WHERE ( feature = ? AND
feature_id IS NULL AND ord = ? ): 'kdkdkdk', '2'
INSERT INTO product_feature (feature, feature_id, ord) VALUES
(?, ?, ?): 'kdkdkdk', 'NULL', '2'
SELECT COUNT( * ) FROM product_feature me WHERE ( feature = ? AND
feature_id IS NULL AND ord = ? ): 'jjjj', '3'
INSERT INTO product_feature (feature, feature_id, ord) VALUES
(?, ?, ?): 'jjjj', 'NULL', '3'
As you can see, we are not selecting product_id from product prior to
putting features in. What should I do to avoid this problem? The
documentation leads me to believe that create() should work for what
I want, so I am guessing, I am doing something wrong.
In addition, why are there so many queries? What is count(*) being
selected for and why are we doing one insert at a time instead of a
bulk insert? Is that something I should improve on?
Thanks for your help!
Kate
More information about the DBIx-Class
mailing list