Subject: | Bug with the mysql driver and using a foreign key relationship with the Sessions table |
Date: | Tue, 24 Jun 2008 10:19:40 -0700 |
To: | bug-CGI-Session [...] rt.cpan.org |
From: | Steve Kirkup <skirkup [...] jupiterimages.com> |
Howdy,
I was implementing something to help 'Customer Service' do
administration with a customers session. To do this I created this table,
CREATE TABLE Customer (
customer_id CHAR(10) PRIMARY KEY,
session_id CHAR(32) NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions(id)
) ENGINE = INNODB;
So I am using the 'sessions' table for CGI::Session to store stuff. When
I go to flush() the CGI::Session I get this error:
Cannot delete or update a parent row: a foreign key constraint fails
(`White_Label/Customer`, CONSTRAINT `Customer_ibfk_1` FOREIGN KEY
(`session_id`) REFERENCES `sessions` (`id`)) at
/usr/local/share/perl/5.8.8/CGI/Session/Driver/mysql.pm line 50.
Looking at line 50 of the mysql.pm file is this,
$dbh->do("REPLACE INTO " . $self->table_name . " ($self->{IdColName},
$self->{DataColName}) VALUES(?, ?)", undef, $sid, $datastr)
or return $self->set_error( "store(): \$dbh->do failed " .
$dbh->errstr );
So what the replace is supposed to is delete the original row, if there
is one, and then insert a new one. Which is causing my problem. Could I
recommend instead that you use the |INSERT ... ON DUPLICATE KEY UPDATE
syntax instead? This will not delete a row, which keeps consistency
with FOREIGN KEYS.
|http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Steve Kirkup
--
Steve Kirkup
Sr Web Developer
520-881-8101 ext 119