Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andig opened this issue Jun 8, 2021 · 3 comments

Comments

@andig
Copy link

andig commented 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.

@greg0ire
Copy link
Member

Duplicate of #8750

@greg0ire greg0ire marked this as a duplicate of #8750 Jun 10, 2021
@andig
Copy link
Author

andig commented 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?

@greg0ire
Copy link
Member

My bad sorry, read too fast

@greg0ire greg0ire reopened this Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants