[Dbix-class] Nested create with relationships
Matt S Trout
dbix-class at trout.me.uk
Sun Apr 20 15:06:29 BST 2008
On Thu, Apr 17, 2008 at 08:13:40PM -0700, Kate Yoak wrote:
> 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:
That's ... odd. Can you send us the classes? In the case of the systems
involved it should do the insert of the main record first, then the
has_many (which, see below, it -does-), but somehow it doesn't have the PK
afterwards.
> 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?
The extra SELECTs are because DBIC hasn't worked out that the has_many
records you've provided are definitely new and is going to check that
they don't already exist so it doesn't accidentally cause a unique
key violation.
In this case it should probably spot that there's no possible unique key
to violate and not try the SELECTs; given that we could -probably- also
do a bulk insert, but most databases don't let you get an auto-inc key
in bulk insert so I'm not sure how we'd distinguish whether that's safe
or not?
--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/
More information about the DBIx-Class
mailing list