in reply to Inserting a relation into a database with Class::DBI

# is this a requirement or can both records be created at once? #my $session_rec = MyDbase::Session->insert({ my $session_rec = { 'id' => $session_id, 'timestamp' => time, }; #});
You create a hashref called $session_rec which probably should be passed to insert like so

my $session_rec = MyDbase::Session->insert( my $session_rec = { 'id' => $session_id, 'timestamp' => time, }; );

Notice that I've removed a pair of braces from your called to insert that were attempting to create a hashref from $session_rec which is already a hashref.

When I try to do something similar here, I get the error I expected.

my $hr2 = { my $hr1 = { A => 1, B => 2 } }; #Odd number of elements in anonymous hash at /home/funky/play line 46.

Standard Disclaimer.txt

I haven't used DBI::Class in a long while, so I may be a mile off the mark.

Replies are listed 'Best First'.
Re^2: Inserting a relation into a database with Class::DBI
by wojtyk (Friar) on May 29, 2007 at 16:39 UTC
    Though I appreciate the response, this doesn't really address my primary question.

    Namely, I only want a single insert taking place in this code (instead of one insert each for $session_rec and $user_rec). Since there's no need to touch the database twice, it feels dirty to me to issue two queries for an action that could be done in parallel.

    Is there a way to compress the inserts into a single call (or at least a single database access)? Since $session_rec has a has_a() relationship with $user_rec, I don't see why DBI::Class wouldn't be smart enough to handle this.

      I don't know of any easy way to do what you want in one query in C::DBI. Generally, if you are concerned about performance, particularly the number of queries being executed, you should probably be looking at an alternative to C::DBI, as it regularly executes lots of queries where a smaller number would suffice. I don't have any specific recommendations for alternatives, but I know there are a few others out there.
      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'