Symfony Doctrine无法创建外键

时间:2015-05-06 16:02:58

标签: mysql symfony doctrine foreign-keys

Ordertaxi中的字段:

@ORM\Column(name="custom_id", type="integer", options={"unsigned"=true})
private $customId;

// new "column", which i want to add
@ORM\OneToOne(targetEntity="CustomOrder")
private $custom;

CustomOrder中的字段:

@ORM\Column(name="id", type="integer")
@ORM\Id
@ORM\GeneratedValue(strategy="AUTO")
private $id;

执行:

php app/console doctrine:schema:update --dump-sql

获得:

ALTER TABLE Ordertaxi ADD CONSTRAINT FK_D345B7FB614A603A FOREIGN KEY (custom_id) REFERENCES CustomOrder (id);
CREATE UNIQUE INDEX UNIQ_D345B7FB614A603A ON Ordertaxi (custom_id)

执行:

php app/console doctrine:schema:update --force

获得:

[Doctrine\DBAL\DBALException]                                                                                                                       
  An exception occurred while executing 'ALTER TABLE Ordertaxi ADD CONSTRAINT FK_D345B7FB614A603A FOREIGN KEY (custom_id) REFERENCES CustomOrder (id  
  )':                                                                                                                                                 
  SQLSTATE[HY000]: General error: 1005 Can't create table 'aerotaxi.#sql-3b8a_4c' (errno: 150) 

为什么此查询会导致错误?

1 个答案:

答案 0 :(得分:1)

将此代码添加到Ordertaxi

@JoinColumn(name="customOrder_id", referencedColumnName="id")