S
S
Svetlana Galenko2019-02-01 22:24:40
Yii
Svetlana Galenko, 2019-02-01 22:24:40

Can't run migrations?

Hello, I can't run migrations

php yii migrate [email protected]/ricco/yii2-ticket/migrations
I'm getting this error, please help me to solve it:
.Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
The SQL being executed was: ALTER TABLE `pref_ticket_head` ADD CONSTRAINT `fk_ticket_head` FOREIGN KEY (`user_id`) REFERENCES `pref_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE'

in /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/db/Schema.php:664

Error Info:
Array
(
    [0] => HY000
    [1] => 1215
    [2] => Cannot add foreign key constraint
)

Stack trace:
#0 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/db/Command.php(1295): yii\db\Schema->convertException(Object(PDOException), 'ALTER TABLE `pr...')
#1 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/db/Command.php(1091): yii\db\Command->internalExecute('ALTER TABLE `pr...')
#2 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/db/Migration.php(467): yii\db\Command->execute()
#3 /var/www/u0598324/public_html/webstels.com/vendor/ricco/yii2-ticket/migrations/m160618_215837_create_ticket_head_table.php(36): yii\db\Migration->addForeignKey('fk_ticket_head', '{{%ticket_head}...', 'user_id', '{{%user}}', 'id', 'CASCADE', 'CASCADE')
#4 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(725): m160618_215837_create_ticket_head_table->up()
#5 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(199): yii\console\controllers\BaseMigrateController->migrateUp('m160618_215837_...')
#6 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#7 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#9 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('', Array)
#10 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('', Array)
#11 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate', Array)
#12 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate', Array)
#13 /var/www/u0598324/public_html/webstels.com/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#14 /var/www/u0598324/public_html/webstels.com/yii(20): yii\base\Application->run()
#15 {main}

Here is a screenshot:
5c549cd7c2055467050653.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2019-02-01
@swallow_97

Your problem is that the field you assign the key to has an UNSIGNED attribute. Remove it and you'll be fine!

G
grinat, 2019-02-01
@grinat

In the same place it is written that it cannot add a foreign key. Judging by your screenshot, it can be assumed that this is due to the fact that the pref_user table does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question