无法添加外键约束 - Yii 2控制器

时间:2017-01-16 13:17:01

标签: php yii2

我正在尝试使用控制器方法创建数据库表。每次运行我的代码时,都会收到以下错误消息。 我正在使用Yii2 Framework

  

SQLSTATE [HY000]:常规错误:1215无法添加外键约束   正在执行的SQL是:CREATE TABLE nametype   varchar(64)not null,description int not null,rule_name text,   data varchar(64),created_at text,updated_at int not null,   actionInstall int not null)CHARACTER SET utf8 COLLATE utf8_unicode_ci   ENGINE = InnoDB错误信息:数组(       [0] => HY000       [1] => 1215       [2] =>无法添加外键约束)

     

引起:PDOException

     

SQLSTATE [HY000]:常规错误:1215无法添加外键约束

     

在   /home/offneo/Projects/MODULEMANAGER/Product/Software/vendor/yiisoft/yii2/db/Command.php   在第844行

以下是控制器方法$db = Yii::$app->db; $tableOptions = null; $status = true; if ($db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } // Table auth_rule if ( $db->getTableSchema('auth_rule') === null ) { $status &= (bool) $db->createCommand()->createTable('auth_rule', [ 'name' => 'varchar(64) not null', 'data' => "text", 'created_at' => "int", 'updated_at' => "int", 'PRIMARY KEY (name)' ], $tableOptions)->execute(); $status &= (bool) $db->createCommand()->createIndex('name_UNIQUE', 'auth_rule', 'name', true)->execute(); } // Table auth_item if ( $db->getTableSchema('auth_item') === null ) { $status &= (bool) $db->createCommand()->createTable('auth_item', [ 'name' => 'varchar(64) not null', 'type'=>'int not null', 'description' => 'text', 'rule_name' => "varchar(64)", 'data'=>'text', 'created_at' => 'int not null', 'updated_at' => 'int not null' ], $tableOptions)->execute(); $status &= (bool) $db->createCommand()->addPrimaryKey('name_pk', 'auth_item', 'name')->execute(); // $status &= (bool) $db->createCommand()->addForeignKey('fk_auth_item_auth_rule1_idx', 'auth_item', 'rule_name', 'auth_rule', 'name', 'SET NULL', 'CASCADE')->execute(); // $status &= (bool) $db->createCommand()->createIndex('fk_auth_item_auth_rule1_idx', 'auth_item', 'name', false)->execute(); }

中的代码
{{1}}

0 个答案:

没有答案