无法在具有不同 CHARSET 的 2 个表之间创建外键

Cant create Foreign Key between 2 tables with different CHARSET

跨不同字符集的外键不起作用

我正在使用 Symfony 和 Doctrine 创建一个新项目。但是旧的 tables 具有不同于 UTF-8 的默认字符集。我的新 table 有 UTF8 字符集。恢复.. 我有一个 table DEFAULT CHARSET = latin1 和一个新的 table in UTF-8.

UTF8table和latin1table有关系。但我收到下一个错误:

MySQL: Error 1005; Can't create table (errno: 150)

有什么方法可以在具有不同 CHARSET 的 table 之间创建外键? 或者,我可以用 ORM 级别来做吗?

There is some way to make a foreign key between tables with different CHARSET ?

在 MySQL 级别没有没有。

Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same

来源:http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

嗯,有一种方法可以做到,但不是 SQL 级别。如果您因为使用 Doctrine 而可以删除表中的关系,请尝试在 ORM 级别建立关系。

喜欢这个例子

(A Ticket 有一个 User 并且 User 有一个 门票.一对一关系):

class Ticket
{
 /**
  * @var \AppBundle\Entity\User
  *
  * @ORM\OneToOne(targetEntity="AppBundle\Entity\User")
  * @ORM\JoinColumn(name="adeq_user", referencedColumnName="id")
  */
 private $adeqUsers;
}

然后 Doctrine 为你建立关系,但 table 是相同的,所以当你要票时,你会得到属于的用户。

无论 db 上的默认字符集如何