Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Catalyst and DBIC, lookup table question

by vendion (Scribe)
on Sep 11, 2011 at 02:29 UTC ( [id://925314]=perlquestion: print w/replies, xml ) Need Help??

vendion has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a Catalyst app and to add data to three different tables in my database. Two of the three tables are for data and the third is a lookup table that uses the uniq ID from the other two tables. I can't seem to find a working method to populating the look up table.

This is what my database tables I am working with look like:

CREATE TABLE computer ( compid integer NOT NULL, compman character varying(128) NOT NULL, compmodel character varying(128) NOT NULL, modnum character varying(128) NOT NULL, os character varying(128) NOT NULL, errormsg character varying(128) NOT NULL, service character varying(128) NOT NULL, probdesc character varying(128) NOT NULL, diddiagnostic boolean DEFAULT false NOT NULL, complete smallint DEFAULT (0)::smallint NOT NULL, loginpw character varying(128) NOT NULL, comment character varying(255) NOT NULL, compsn character varying(128) NOT NULL, date timestamp NOT NULL, lastmod timestamp DEFAULT now() ); CREATE TABLE customers ( uniqid integer NOT NULL, firstname character varying(255) NOT NULL, lastname character varying(255) NOT NULL, email character varying(255) NOT NULL, phone character varying(13) NOT NULL, date timestamp NOT NULL, isbusstu boolean DEFAULT false NOT NULL, donations integer NOT NULL ); CREATE TABLE customerstocomputers ( custid integer NOT NULL, compid integer NOT NULL );

As for what I am trying to do is create the records in the customers and computer table then pull the data back to get the uniqe IDs (because they are auto incremented from Postgre)

my $newperson = eval { $customer_rs->create({ firstname => $params->{firstname}, lastname => $params->{lastname}, email => $params->{email}, phone => $params->{phone}, date => $date, donations => '0', }) }; my $newcomputer = eval { $computer_rs->create({ compman => $params->{compman}, compmodel => $params->{compmodel}, modnum => $params->{modnum}, os => $params->{os}, errormsg => $params->{errormsg}, service => $params->{service}, probdesc => $params->{probdesc}, loginpw => $params->{loginpw}, comment => $params->{comment}, compsn => $params->{compsn}, date => $date, }) }; my $customer = $customer_rs->find({ email => $params->{email} }); my $computer = $computer_rs->find({ compsn => $params->{compsn} }); eval { $c2c_rs->create({ custid => $customer->{uniqid}, compid => $computer->{compid}, }) };

Using Data::Dumper I can see that $customer and $computer is populated but how I am accessing them returns undef. What am I doing wrong here, or is there a better way to achieve my goal?

Replies are listed 'Best First'.
Re: Catalyst and DBIC, lookup table question
by keszler (Priest) on Sep 11, 2011 at 04:05 UTC
    I'm having to make several assumptions here, but what do $customer->uniqid and $computer->compid return?

      That was it, I was thinking of them as a hashref so I thought it needed the the {}, thank you for your help

Re: Catalyst and DBIC, lookup table question
by NetWallah (Canon) on Sep 11, 2011 at 05:28 UTC
    You did not say what database you were using, but it strikes me as strange that the following attributes are missing from your ID fields:
    • PRIMARY_KEY
    • AUTO_INCREMENT

                "XML is like violence: if it doesn't solve your problem, use more."

      I did state in my OP that I was using "Postgre", and the PRIMARY KEY assignment is done further down in the SQL dump, in typical Postgresql fashion.

      ALTER TABLE computer ALTER COLUMN compid SET DEFAULT nextval('computer +_compid_seq'::regclass); ALTER TABLE customers ALTER COLUMN uniqid SET DEFAULT nextval('custome +rs_uniqid_seq'::regclass); ALTER TABLE ONLY computer ADD CONSTRAINT computer_pkey PRIMARY KEY (compid); ALTER TABLE ONLY customers ADD CONSTRAINT customers_pkey PRIMARY KEY (uniqid); ALTER TABLE ONLY customerstocomputers ADD CONSTRAINT customerstocomputers_pkey PRIMARY KEY (custid, comp +id); ALTER TABLE ONLY customerstocomputers ADD CONSTRAINT customerstocomputers_custid_fkey FOREIGN KEY (custi +d) REFERENCES customers(uniqid); ALTER TABLE ONLY customerstocomputers ADD CONSTRAINT customerstocomputers_compid_fkey FOREIGN KEY (compi +d) REFERENCES computer(compid);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://925314]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found