Another Strange MySQL Problem

Tim Chase python.list at tim.thechases.com
Fri May 21 14:40:49 EDT 2010


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.

-tkc






More information about the Python-list mailing list