Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I traced the source of the problem. When creating 2 InnoDB tables with a foreign key relationship, the foreign key column must be indexed explicitly <a href="http://dev.mysql.com/doc/refman/4.1/en/innodb-foreign-key-constraints.html" rel="nofollow">prior to MySQL 4.1.2</a>. Using Django's ORM, this can be done by using the <code>db_index=True</code> option in the foreign key field. However, in the Django generated SQL, the <code>CREATE INDEX</code> statement is issued after the foreign key relationship is created. For example, for the following models:</p> <pre><code>class Customer(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) class Order(models.Model): customer = models.ForeignKey(Customer, db_index=True) </code></pre> <p>Django generates the following SQL code:</p> <pre><code>BEGIN; CREATE TABLE `foo_app_customer` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `first_name` varchar(100) NOT NULL, `last_name` varchar(100) NOT NULL ) ; CREATE TABLE `foo_app_order` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `customer_id` integer NOT NULL ) ; ALTER TABLE `foo_app_order` ADD CONSTRAINT `customer_id_refs_id_27e4f922` FOREIGN KEY (`customer_id`) REFERENCES `foo_app_customer` (`id`); CREATE INDEX `foo_app_order_12366e04` ON `foo_app_order` (`customer_id`); COMMIT; </code></pre> <p>If you try running this code using MySQL 4.0, an errno 150 will occur when trying to execute the <code>ALTER TABLE</code> statement. But if the <code>CREATE INDEX</code> statement is issued first, everything works like a charm. As far as I can tell, the only workaround for this is to create your own table manually and using <code>inspectdb</code> afterwards to generate the models. </p> <p>Also, I created a new <a href="https://code.djangoproject.com/ticket/16823" rel="nofollow">Django ticket</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload