orm:schema-tool:update Execution plan fails due to ignoring existing fk constraints (mysql) #6749

Open
opened 2026-01-22 15:37:58 +01:00 by admin · 3 comments
Owner

Originally created by @andig on GitHub (Jun 8, 2021).

I'm converting the following entity from using dedicated PK to composite PK by removing the id column. From:

/**
* @Entity
* @Table(
* 	name="data",
*	uniqueConstraints={@UniqueConstraint(name="data_unique", columns={"channel_id", "timestamp"})}
* )
*/
class Data
{
    /**
    * @Id
    * @Column(type="integer", nullable=false)
    * @GeneratedValue(strategy="IDENTITY")
    */
    protected $id;

    /**
    * @Column(type="bigint", nullable=false)
    */
    protected $timestamp;

    /**
    * @Column(type="float", nullable=false)
    */
    protected $value;

    /**
    * @ManyToOne(targetEntity="Channel", inversedBy="data")
    * @JoinColumn(name="channel_id", referencedColumnName="id", nullable=false)
    */
    protected $channel;
}

To:

/**
* @Entity
* @Table(name="data")
*/
class Data
{
    /**
    * @Id
    * @ManyToOne(targetEntity="Channel", inversedBy="data")
    * @JoinColumn(name="channel_id", referencedColumnName="id")
    */
    protected $channel;

    /**
    * @Id
    * @Column(type="bigint")
    */
    protected $timestamp;

    /**
    * @Column(type="float", nullable=false)
    */
    protected $value;
}

This creates the following execution plan:

ALTER TABLE data MODIFY id INT NOT NULL;
DROP INDEX data_unique ON data;
ALTER TABLE data DROP PRIMARY KEY;
ALTER TABLE data DROP id, CHANGE channel_id channel_id INT NOT NULL;
ALTER TABLE data ADD PRIMARY KEY (channel_id, timestamp);

The plan fails intermittendly due to

In PDOConnection.php line 132:

  [PDOException (HY000)]
  SQLSTATE[HY000]: General error: 1832 Cannot change column 'channel_id': used in a foreign key constraint 'FK_ADF3F36372F5A1AA'

It seems that ManyToOne Fk relation is not handled properly when changing the underlying columns.

Originally created by @andig on GitHub (Jun 8, 2021). I'm converting the following entity from using dedicated PK to composite PK by removing the id column. From: /** * @Entity * @Table( * name="data", * uniqueConstraints={@UniqueConstraint(name="data_unique", columns={"channel_id", "timestamp"})} * ) */ class Data { /** * @Id * @Column(type="integer", nullable=false) * @GeneratedValue(strategy="IDENTITY") */ protected $id; /** * @Column(type="bigint", nullable=false) */ protected $timestamp; /** * @Column(type="float", nullable=false) */ protected $value; /** * @ManyToOne(targetEntity="Channel", inversedBy="data") * @JoinColumn(name="channel_id", referencedColumnName="id", nullable=false) */ protected $channel; } To: /** * @Entity * @Table(name="data") */ class Data { /** * @Id * @ManyToOne(targetEntity="Channel", inversedBy="data") * @JoinColumn(name="channel_id", referencedColumnName="id") */ protected $channel; /** * @Id * @Column(type="bigint") */ protected $timestamp; /** * @Column(type="float", nullable=false) */ protected $value; } This creates the following execution plan: ALTER TABLE data MODIFY id INT NOT NULL; DROP INDEX data_unique ON data; ALTER TABLE data DROP PRIMARY KEY; ALTER TABLE data DROP id, CHANGE channel_id channel_id INT NOT NULL; ALTER TABLE data ADD PRIMARY KEY (channel_id, timestamp); The plan fails intermittendly due to ``` In PDOConnection.php line 132: [PDOException (HY000)] SQLSTATE[HY000]: General error: 1832 Cannot change column 'channel_id': used in a foreign key constraint 'FK_ADF3F36372F5A1AA' ``` It seems that `ManyToOne` Fk relation is not handled properly when changing the underlying columns.
Author
Owner

@greg0ire commented on GitHub (Jun 10, 2021):

Duplicate of #8750

@greg0ire commented on GitHub (Jun 10, 2021): Duplicate of #8750
Author
Owner

@andig commented on GitHub (Jun 10, 2021):

@greg0ire this is not a duplicate. #8750 is about planning redundant schema updates while this issue is about planning schema updates that will fail as constraints are not taken into account.

Could you please reopen?

@andig commented on GitHub (Jun 10, 2021): @greg0ire this is not a duplicate. #8750 is about planning redundant schema updates while this issue is about planning schema updates that will fail as constraints are not taken into account. Could you please reopen?
Author
Owner

@greg0ire commented on GitHub (Jun 10, 2021):

My bad sorry, read too fast

@greg0ire commented on GitHub (Jun 10, 2021): My bad sorry, read too fast
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6749