Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Bug x-indexes with $ref-attribute #160

Open
@siggi-k

Description

@siggi-k

Error with migrate down

Initial state:
schema.yaml

  schemas:

    InvoiceDetail:
      type: object
      required:
        - id
        - customer
        - countryCode
        - postCode
      x-indexes:
        - 'unique:customer,countryCode,postCode'
      properties:
        id:
          type: integer
        customer:
          $ref: '#/components/schemas/Customer'
        countryCode:
          type: integer
        postCode:
          type: integer

Terminal:
./yii gii/api
generate this:

m240130_080000_create_table_invoice_details.php

/**
 * Table for InvoiceDetail
 */
class m240130_080000_create_table_invoice_details extends \yii\db\Migration
{
    public function up()
    {
        $this->createTable('{{%invoice_details}}', [
            'id' => $this->primaryKey(),
            'customer_id' => $this->integer()->notNull(),
            'countryCode' => $this->integer()->notNull(),
            'postCode' => $this->integer()->notNull(),
        ]);
        $this->addForeignKey('fk_invoice_details_customer_id_customers_id', '{{%invoice_details}}', 'customer_id', '{{%customers}}', 'id');
        $this->createIndex('invoice_details_customer_id_countryCode_postCode_key', '{{%invoice_details}}', 'customer_id,countryCode,postCode', true);
    }

    public function down()
    {
        $this->dropIndex('invoice_details_customer_id_countryCode_postCode_key', '{{%invoice_details}}');
        $this->dropForeignKey('fk_invoice_details_customer_id_customers_id', '{{%invoice_details}}');
        $this->dropTable('{{%invoice_details}}');
    }
}

./yii migrate -> works correctly

Reproduction steps:
./yii migrate/down -> gives errors

Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1553 Cannot drop index 'invoice_details_customer_id_countryCode_postCode_key': needed in a foreign key constraint
The SQL being executed was: DROP INDEX `invoice_details_customer_id_countryCode_postCode_key` ON `invoice_details`'

in /app/vendor/yiisoft/yii2/db/Schema.php:676

Error Info:
Array
(
    [0] => HY000
    [1] => 1553
    [2] => Cannot drop index 'invoice_details_customer_id_countryCode_postCode_key': needed in a foreign key constraint
)

Caused by: Exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1553 Cannot drop index 'invoice_details_customer_id_countryCode_postCode_key': needed in a foreign key constraint'

in /app/vendor/yiisoft/yii2/db/Command.php:1302

P.S.

But if you first
$this->dropForeignKey('fk_invoice_details_customer_id_customers_id', '{{%invoice_details}}');
and only then
$this->dropIndex('invoice_details_customer_id_countryCode_postCode_key', '{{%invoice_details}}');
then it would work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions