yii2-openapi
yii2-openapi copied to clipboard
Bug x-indexes with $ref-attribute
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.
This issue is fixed in https://github.com/cebe/yii2-openapi/pull/171. Commit.
Please upgrade your lib to version which has changes of above PR.
If you still see this issue, feel free to comment here.