B
B
BonBon Slick2020-07-09 09:27:51
Doctrine ORM
BonBon Slick, 2020-07-09 09:27:51

Foreign key constraint is incorrectly formed?

$this->addSql('ALTER TABLE user_notification_settings ADD CONSTRAINT FK_7051D51E539B0606 FOREIGN KEY (uid) 
REFERENCES useraccounts_v3 (id)');

        $this->addSql('ALTER TABLE user_notification_settings ADD CONSTRAINT FK_7051D51E4FBDA576 FOREIGN KEY (eid) 
REFERENCES events (id)');

        $this->addSql('CREATE INDEX IDX_7051D51E539B0606 ON user_notification_settings (uid)');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_7051D51E4FBDA576 ON user_notification_settings (eid)');

[notice] Migrating up to DoctrineMigrations\Version20200709062413
[error] Migration DoctrineMigrations\Version20200709062413 failed during Execution. Error: "An exception occurred while 
executing 'ALTER TABLE user_notification_settings ADD CONSTRAINT FK_7051D51E4FBDA576 FOREIGN KEY (eid) REFERENCES events (id)':

SQLSTATE[HY000]: General error: 1005 Can't create table `event_app`.`#sql-339_28` (errno: 150 "Foreign key constraint is incorrectly formed")"

In AbstractMySQLDriver.php line 106:
                                                                                                                                                               
  An exception occurred while executing 'ALTER TABLE user_notification_settings ADD CONSTRAINT 
FK_7051D51E4FBDA576 FOREIGN KEY (eid) REFERENCES events (id)':  
                                                                                                                                                               
  SQLSTATE[HY000]: General error: 1005 Can't create table `event_app`.`#sql-339_28` (errno: 150 "Foreign key constraint is incorrectly formed")                
                                                                                                                                                               

In PDOConnection.php line 43:
                                                                                                                                                 
  SQLSTATE[HY000]: General error: 1005 Can't create table `event_app`.`#sql-339_28` (errno: 150 "Foreign key constraint is 

incorrectly formed")  
                                                                                                                                                 

In PDOConnection.php line 41:
                                                                                                                                                 
  SQLSTATE[HY000]: General error: 1005 Can't create table `event_app`.`#sql-339_28` (errno: 150 "Foreign key constraint is incorrectly formed")

/**
     * @var User
     *
     * @ORM\ManyToOne(
     *     targetEntity="App\Entity\User\DIgnore\User",
     * )
     * @ORM\JoinColumn(name="uid", referencedColumnName="id")
     *
     * @Groups({"all"})
     */
    private $owner;


    /**
     * @var Event
     *
     * @ORM\OneToOne(targetEntity="App\Entity\Event")
     * @ORM\JoinColumn(name="eid", referencedColumnName="id")
     *
     * @Groups({"all"})
     */
    private $event;


Throws an error on the Event $event field.
events -> id (int 11)
Field eid (int 11)
/**
 * Event
 *
 * @ORM\Table(name="events")
 * @ORM\Entity(repositoryClass="App\Repository\EventRepository")
 */
class Event
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     *
     * @Groups({"all"})
     */
    private $id;

event_app is generally the name of the database. There is no creation of such a table in migrations.
$this->addSql(
            'CREATE TABLE IF NOT EXISTS events (id INT AUTO_INCREMENT NOT NULL, ....., PRIMARY KEY(id)) DEFAULT 
CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM COMMENT = \'\' '
        );

        $this->addSql(
            'CREATE TABLE IF NOT EXISTS `user_notification_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL,
  `eid` int(11) DEFAULT NULL,
  ...,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;'
        );

setting a Foreign Key for the User $owner field works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2020-07-09
@BonBonSlick

Here's what I noticed, the difference is ENGINE tables

PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = MyISAM COMMENT = \'\' '
        );

doctrine default engine InnoDB, and since the project is legacy, the new migration had an incorrect ENGINE for the table. To do this, you need to ask the system administrator or who has access to the DB prod what type of table they have, ideally ask for the entire sql.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question