php - yii迁移返回1215 mysql错误

标签 php mysql sql yii migration

一遍又一遍地搜索和检查我的代码后,现在我想问一下。
我有 2 个表 umessageureply,当我想将外键从 ureply 引用添加到 umessage 时,我得到了1215 mysql 错误。

文件m140602_080318_create_table_umessage.php中创建umessage表的代码:

public function safeUp()
{
    /*
     * Create table umessage, this is connection way between customer & seller about specific object
     * Add foreign key to table user and it's column id with sender column
     * Add foreign key to table object and it's column id with objId column
     */
    $this->createTable('tbl_umessage', array(
        'id' => 'pk',
        'time' => 'INT(15) NOT NULL',
        'body' => 'TEXT NOT NULL',
        'status' => 'TINYINT NOT NULL DEFAULT 0',
        'visibleToS' => 'TINYINT NOT NULL DEFAULT 0',
        'visibleToR' => 'TINYINT NOT NULL DEFAULT 0',
        'sender' => 'INT(11)',
        'objId' => 'INT(11) NOT NULL',
    ), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');
}  

文件m140602_080329_create_table_ureply.php中的代码创建ureply表:

public function safeUp()
{
    /*
     * Create table ureply which store all replies to exact message
     * Add foreign key to table umessage and it's column id with msgId column
     */
    $this->createTable('tbl_ureply', array(
        'id' => 'pk',
        'time' => 'INT(15) NOT NULL',
        'body' => 'TEXT NOT NULL',
        'isSender' => 'TINYINT NOT NULL DEFAULT 0',
        'msgId' => 'INT(11) NOT NULL',
    ), 'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');

    $this->addForeignKey('fk_ureply_umessage', 'tbl_ureply', 'msgId', 'umessage', 'id', 'CASCADE', 'CASCADE');
} 

1215 错误是由于添加 fk_ureply_umessage 外键而导致的,我找不到我的错误。 任何帮助将不胜感激。提前致谢。

最佳答案

您在 addForeignKey 方法中出现错误:

$this->addForeignKey('fk_ureply_umessage', 'tbl_ureply', 'msgId', 'umessage', 'id', 'CASCADE', 'CASCADE');

外键引用的表应该是tbl_umessage而不是umessage:

$this->addForeignKey('fk_ureply_umessage', 'tbl_ureply', 'msgId', 'tbl_umessage', 'id', 'CASCADE', 'CASCADE');

关于php - yii迁移返回1215 mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24031995/

相关文章:

php - 如何检查字符串是否是有效的 XML 元素名称?

php - 获取特定分类术语的高级自定义字段组

php - Cron 作业不适用于 php 函数?

java - 处理 java.lang.NullPointerException 错误

php - 按名称获取元素 - HTML DOM 短语

php - WordPress 中的 mySQL 随机顺序查询有 1 个异常(exception)

php - 尝试让我的登录代码正常工作

php - 用户交互式刷新动态菜单

mysql - 在mysql中将日期转换为UTC时间戳

PHP MySQL查询以检查记录是否存在