Skip to content

Column collation is missing on foreign keys #2811

@Tobion

Description

@Tobion

There are two bugs with column collations:

  1. schema update / migrations doesn't recognize collation changes, see Detect changes on the column collation (Issue #2400) #2401 similar issues
  2. column collation doesn't work correctly when used on a field referenced as a forgein key.

This tickets is about 2)

When you have a column collation overwritten for an entity primary key that is used as foreign key in another entity, then the collation is MISSING in the foreign key definition. This makes the generated schema invalid because the collations can be different which is not allowed in foreign key constraints.

Example:

class Order
{
    /**
     * @var string
     *
     * @ORM\Column(type="string", length=8, options={"fixed"=true, "collation"="ascii_general_ci"})
     * @ORM\Id
     */
    private $id;
class OrderItem
{
    /**
     * @var Order
     *
     * @ORM\ManyToOne(
     *     targetEntity="AppBundle\Entity\Order",
     *     inversedBy="items"
     * )
     */
    private $order;

The generated schema for Order is correct: id CHAR(8) NOT NULL COLLATE ascii_general_ci
But OrderItem creates an invalid schema because it is missing the collation of the order primary key: order_id CHAR(8) NOT NULL The default collation of the table is used, which can be a totally different one.

When it then tries to add the foreign key constraint it fails with SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions