开发者

Strange foreign key issue with MySQL 5.5.9

I'm experiencing a rather strange problem with foreign开发者_如何学Go keys, namely this doesn't work as expected:

ALTER TABLE contact_contactlist 
  ADD FOREIGN KEY (contact_id) REFERENCES contacts(id) ON DELETE CASCADE;

ALTER TABLE contact_contactlist 
  ADD FOREIGN KEY (contactlist_id) REFERENCES contactLists(id) ON DELETE CASCADE;

ALTER TABLE contactLists 
  ADD FOREIGN KEY (owner_id) REFERENCES serviceAccounts(id);

(auto-generated by Doctrine 2)

Yet, this works perfectly:

ALTER TABLE `contact_contactlist`
  ADD CONSTRAINT `contact_contactlist_ibfk_5` 
    FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `contact_contactlist_ibfk_6` 
    FOREIGN KEY (`contactlist_id`) REFERENCES `contactLists` (`id`) ON DELETE CASCADE;  
ALTER TABLE `contactLists`
  ADD CONSTRAINT `contactlists_ibfk_1` 
    FOREIGN KEY (`owner_id`) REFERENCES `serviceAccounts` (`id`);

(phpMyAdmin export of the very same keys above)

The problem manifests itself as MySQL won't let me insert anything into the tables if the keys are defined with the SQL from the first block, complaining on referential integrity (even though the referenced entries exist), yet all is well if I set the keys with the SQL from the second block.

I am aware that I can just "go on and be happy" using the latter key definition SQL, but I'd prefer if I could somehow just stick to the Doctrine-generated SQL as I'm still early in development and model will change quite often (thus the two-step manual key fixing will become rather annoying).

EDIT

Here are the create statements

CREATE TABLE contact_contactlist (
  contact_id BIGINT NOT NULL, 
  contactlist_id BIGINT NOT NULL, 
  INDEX contact_contactlist_contact_id_idx (contact_id), 
  INDEX contact_contactlist_contactlist_id_idx (contactlist_id), 
  PRIMARY KEY(contact_id, contactlist_id)) 
ENGINE = InnoDB; 

CREATE TABLE contactLists (
  id BIGINT AUTO_INCREMENT NOT NULL, 
  owner_id INT DEFAULT NULL, 
  name VARCHAR(255) NOT NULL, 
  INDEX contactLists_owner_id_idx (owner_id), 
  PRIMARY KEY(id)) 
ENGINE = InnoDB;


If you're using Mac OS X, you might be running into a bug in MySQL:
MySQL 5.5 foreign key constraint fails when foreign key exists

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜