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

I really need the help of the Perl/Oracle gurus out there :( I have this program that is trying to do an insert on a table that has two TIMESTAMP fields. Here's the consolidated code:

use strict; use DBI; $ENV{'ORACLE_HOME'} = '/pkg/oracle/client10g'; my $sid = 'mysid'; my $user = 'joe'; my $pass = 'joespw'; my $dbh = DBI->connect( "dbi:Oracle:$sid", "$user", "$pass" ) || die( $DBI::errstr . "\n" ); $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; $dbh->{ora_check_sql} = 0; $dbh->{RowCacheSize} = 16; my $sql = qq{insert into reservation (machinename,reservationida,begin +reserve,endreserve,description,reservationid) values (?,?,TO_TIMESTAM +P(?,'DD/MON/YYYY HH:MI:SS'), TO_TIMESTAMP(?,'DD/MON/YYYY HH.MI.SS'),?,?)}; my $sth = $dbh->prepare($sql); $sth->bind_param(1, 'volcano'); $sth->bind_param(2, 666); $sth->bind_param(3, '12/MAR/2010 7:20:00'); $sth->bind_param(4, '12/MAR/2010 8:20:00'); $sth->bind_param(5, 'This machine tends to explode!'); $sth->bind_param(6, 666); #print "$sql\n"; $sth->execute; $sth->finish; $dbh->disconnect;

The database table:

SQL> Name Null? Type ----------------------------------------- -------- ------------------ +---------- RESERVERNAME VARCHAR2(200) RESERVATIONIDA NOT NULL NUMBER(38) BEGINRESERVE NOT NULL TIMESTAMP(6) ENDRESERVE TIMESTAMP(6) DESCRIPTION VARCHAR2(400) MACHINENAME NOT NULL VARCHAR2(20) RESERVATIONID NOT NULL CHAR(50) SQL> Disconnected from Oracle Database 11g Enterprise Edition Release +11.1.0.6.0 - 64bit Production

Here are 2 normal rows from a select statement:

CARRIE,123454,24-NOV-09 06.00.00.000000 PM,24-NOV-09 08.00.00.000000 P +M,For Carrie test,pipe,123454 box1,56789,25-JUL-09 07.00.00.000000 AM,25-JUL-09 08.00.00.000000 AM,, +ector,56789

When I run my script, I get:

DBD::Oracle::st execute failed: ORA-02291: integrity constraint (LRRS. +RESERVATION_MACHINE_FK1) violated - parent key not found (DBD ERROR: +OCIStmtExecute) [for Statement "insert into reservation (machinename, +reservationida,beginreserve,endreserve,description,reservationid) val +ues (?,?,TO_TIMESTAMP(?,'DD/MON/YYYY HH:MI:SS'),TO_TIMESTAMP(?,'DD/MO +N/YYYY HH.MI.SS'),?,?)" with ParamValues: :p1='volcano', :p2=666, :p3 +='12/MAR/2010 7:20:00', :p4='12/MAR/2010 8:20:00', :p5='This is explo +sive fun!', :p6=666] at ./liltest.pl line 23.

This error actually appears twice.

I can connect using sqlplus and insert manually, kind of like this: http://use.perl.org/~jdavidb/journal/29951, so it seems TO_TIMESTAMP should work - actually I'm not even sure if that's the problem. Can't seem to figure this one out... thanks for the help!
DW

Replies are listed 'Best First'.
Re: DBD::Oracle foreign key problem
by keszler (Priest) on Feb 03, 2010 at 15:22 UTC
    The problem is not with the timestamp. There is a foreign key constraint (LRRS.RESERVATION_MACHINE_FK1) that your entry is not meeting.

    Guessing from the constraint name and the reservation table field names, it may be the machinename value you're using or the reservername value you're not using.

Re: DBD::Oracle foreign key problem
by Corion (Patriarch) on Feb 03, 2010 at 15:27 UTC

    It seems that the machine volcano does not exist in some other table, or at least that's what the failing constraint LRRS.RESERVATION_MACHINE_FK1 seems to suggest to me.

    If you can tell us the definition of that constraint, maybe we can see more. Also, showing the actual INSERT statements that work might also help.

      Yep, the host name needed to exist... well somewhere. After 'communicating' with our dba, I'm still left with unanswered questions but, it's not like I got actual requirements/info/love for this project anyway! sigh

      With a little research i'll figure out how to get Oracle to tell me about these mysterious constraints. Maybe table_info()?

      Anyway, thanks, this place is awesome as usual.

Re: DBD::Oracle foreign key problem
by roboticus (Chancellor) on Feb 03, 2010 at 15:37 UTC

    digdoug:

    The error message is complaining about a foreign key constraint, so you might want to check your table definition to find out what column(s) could be the problem. For example, if the reservationid column is FKed to another table, that other table will need the record for this reservationid to be inserted before you can insert this one.

    ...roboticus