V
V
Vitaly2021-04-15 12:32:54
Yii
Vitaly, 2021-04-15 12:32:54

Why does adding foreign keys during migration result in SQLSTATE[HY000]: General error: 1215 and the migration does not complete?

Help, please, to understand a newbie in Yii2, I seem to be doing according to the documentation, but something is wrong.
I'm trying to add a table to the database through migration, the rest of the tables were created MANUALLY.
Perhaps someone tried using migration to the working database (CREATED MANUALLY) to add new tables and prescribe Foregn_key to it?
The types of the id fields from the old tables to which I am trying to bind the fields from the newly created table correspond to the types of the foreign key fields of the new table int(11) , the fields to which the relationship is established by foreign keys are indexed and are primary keys in their tables.
The table in the database is added with all the prescribed fields and types as it should be, the field for the foreign key is indexed, but the binding does not occur.
Accordingly, there are no foreign keys in the migration table, since an error occurs at the key generation stage (from the terminal):
> add foreign key fk-comment-author_id: {{%comment}} (author_id) references {{%user}} (id) ...Exception: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

The SQL being executed was: ALTER TABLE `comment` ADD CONSTRAINT `fk-comment-author_id` FOREIGN KEY (`author_id`) REFERENCES `user` (`id`) ON DELETE CASCADE (D:\open-server\OSPanel\domains\yii2.loc\vendor\yiisoft\yii2\db\Schema.php:678)
#0 D:\open-server\OSPanel\domains \yii2.loc\vendor\yiisoft\yii2\db\Command.php(1304): yii\db\Schema->convertException().......... .........................................

As a result of running the migration in the terminal:
*** failed to apply m210415_065603_create_comment_table (time: 0.644s)
0 from 1 migrations were applied.
Migration failed. The rest of the migrations are cancelled.

In the createTable method, I also tried to set field types using Schema::, but there is no difference.

public function safeUp()
    {
        $this->createTable('{{%comment}}', [
            'id' =>  $this->primaryKey(),
            'author_id' => Schema::TYPE_INTEGER.' NOT NULL',
            'text' => $this->text()->notNull(),
            'parent_id' => $this->integer(),
            'product_id' => $this->integer()->notNull(),
            'moderation' => $this->boolean(),
            'created' => $this->date(),
            'is_admin' => $this->boolean(),
        ]);

      // creates index for column `product_id`
      $this->createIndex(
        'idx-comment-product_id',
        'comment',
        'product_id',
      );

      // add foreign key for table `product` ОШИБКА ПРОИСХОДИТ ВО ВРЕМЯ ЭТОЙ ОПЕРАЦИИ
      $this->addForeignKey(
        'fk-comment-product_id',
        'comment',
        'product_id',
        'product',
        'id',
        'CASCADE',
      );

      // creates index for column `author_id`
      $this->createIndex(
        'idx-comment-author_id',
        'comment',
        'author_id'
      );

      // add foreign key for table `user` ОШИБКА ПРОИСХОДИТ ВО ВРЕМЯ ЭТОЙ ОПЕРАЦИИ
      $this->addForeignKey(
        'fk-comment-author_id',
        '{{%comment}}',
        'author_id',
        '{{%user}}',
        'id',
        'CASCADE'
      );

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {

      // drops foreign key for table `user`
      $this->dropForeignKey(
        'fk-comment-author_id',
        'comment',
      );

      // drops index for column `author_id`
      $this->dropIndex(
        'idx-comment-author_id',
        'comment',
      );

      // drops foreign key for table `product`
      $this->dropForeignKey(
        'fk-comment-product_id',
        'comment',
      );

      // drops index for column `product_id`
      $this->dropIndex(
        'idx-comment-product_id',
        'comment',
      );

        $this->dropTable('{{%comment}}');
    }


THE QUESTION IS CLOSED.
I made migrations of several new tables and immediately registered fields with foreign keys between them, it worked, so it's a hodgepodge of tables from the migration and old database tables.
Thanks to everyone who took part in the search for the problem!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy, 2021-04-16
@Vip7777

I made migrations of several new tables and immediately registered fields with foreign keys between them, it worked, so it's a hodgepodge of tables from the migration and old database tables.
Thanks to everyone who took part in the search for the problem!

D
Dmitry Bay, 2021-04-15
@kawabanga

1) Check the type of database, maybe MyISAM?
2) check that
comment'.product_id'
and
'product'.'id'
are of the same type, 'product_id' => $this->integer()->notNull(), you never know, what if your product is bigint ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question