Another Strange MySQL Problem

Victor Subervi victorsubervi at gmail.com
Sat May 22 09:51:04 EDT 2010


On Fri, May 21, 2010 at 2:40 PM, Tim Chase <python.list at tim.thechases.com>wrote:

> On 05/21/2010 12:31 PM, Victor Subervi wrote:
>
>>     cursor.execute('insert into Baggage values (Null, %s, %s, %s,
>> %s)', (flight_id, customer_id, weight, ticket_no))
>>
>
> You're trying to insert stuff...
>
>
>  OperationalError: (1452, 'Cannot add or update a child row: a foreign
>> key constraint fails (`seaflight/Baggage`, CONSTRAINT `Baggage_ibfk_2`
>> FOREIGN KEY (`customer_id`) REFERENCES `Customers` (`id`))')
>>
>
> But the value you're giving for the customer_id doesn't exist in the
> Customers table (as mandated by the FK).  Or perhaps the column-order for
> Baggage isn't what you think it is.  I always specify it explicitly:
>
>  INSERT INTO Baggage (
>   something, flight_id, customer_id, weight, ticket_no
>  ) VALUES (Null, %s, %s, %s, %s)
>
> just in case the table column-order ever changes during a database update.
>

As it turns out, my error was confusing my tables Customers and Passengers.
I was calling customer_id when I should have been calling passenger_id,
which is why MySQL couldn't find the correct values <:-}
beno

>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100522/089e3a3b/attachment-0001.html>


More information about the Python-list mailing list