无法在magento 2中创建表

时间:2018-12-17 18:11:16

标签: magento magento2

我正在尝试使用安装模式创建表,但会引发以下错误 正在安装模式... SQLSTATE [HY000]:常规错误:1005无法创建表magentoquestionanswer(错误号:150“外键约束格式不正确”),查询为:CREATE  如果不存在questionanswer(   question_id int NULL COMMENT'问题ID',   answer_id int NULL注释“答案ID”,   主键(question_idanswer_id),   索引QUESTIONANSWER_ANSWER_IDanswer_id),   在删除级联上约束QUESTIONANSWER_QUESTION_ID_FAQQUESTION_QUESTION_ID外键(question_id)参考faqquestionquestion_id),   删除级联时约束QUESTIONANSWER_ANSWER_ID_FAQANSWER_ANSWER_ID外键(answer_id)参考faqansweranswer_id) )COMMENT ='回答链接表的问题'ENGINE = INNODB charset = utf8 COLLATE = utf8_general_ci。

我从installschema中获取的表代码是

        //      Creating Question table
    $table = $installer->getConnection()->newTable(
        $installer->getTable('faqquestion')
    )->addColumn(
        'question_id',
        \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
        null,
        ['nullable' => false, 'primary' => true],
        'Question ID'
    )->addColumn(
        'question',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        '2M',
        ['nullable' => true,'default' => null],
        'Question'
    )->addColumn(
        'question_identifier',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Identifier'
    )->addColumn(
        'question_topic',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Topic'
    )->addColumn(
        'question_sortorder',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'SortOrder'
    )->addColumn(
        'question_status',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'Question Status'
    )->addColumn(
        'question_visibility',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'Question Visibility'
    )->addColumn(
        'question_tags',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Tags'
    )->setComment(
        'Question Table'
    );
    $installer->getConnection()->createTable($table);



    //creating answer table
    $table = $installer->getConnection()->newTable(
        $installer->getTable('faqanswer')
    )->addColumn(
        'answer_id',
        \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
        null,
        ['nullable' => false, 'primary' => true],
        'Answer ID'
    )->addColumn(
        'answer',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        '2M',
        ['nullable' => true,'default' => null],
        'Answer'
    )->addColumn(
        'answer_likes',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'Likes'
    )->addColumn(
        'answer_dislikes',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'Dislikes'
    )->addColumn(
        'answer_author',
        \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
        255,
        ['nullable' => true],
        'Author'
    )->addColumn(
        'answer_status',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        1,
        ['nullable' => false,'default' => 0],
        'Answer Status'
    )->setComment(
        'Answer table'
    );
    $installer->getConnection()->createTable($table);

//        queston and related answer table
    $table = $installer->getConnection()->newTable(
        $installer->getTable('questionanswer')
    )->addColumn(
        'question_id',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        null,
        ['nullable' => true, 'primary' => true],
        'Question ID'
    )->addColumn(
        'answer_id',
        \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
        null,
        ['nullable' => true, 'primary' => true],
        'Answer ID'
    )->addIndex(
        $installer->getIdxName('questionanswer', ['answer_id']),
        ['answer_id']
    )->addForeignKey(
        $installer->getFkName('questionanswer', 'question_id', 'faqquestion', 'question_id'),
        'question_id',
        $installer->getTable('faqquestion'),
        'question_id',
        \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
    )->addForeignKey(
        $installer->getFkName('questionanswer', 'answer_id', 'faqanswer', 'answer_id'),
        'answer_id',
        $installer->getTable('faqanswer'),
        'answer_id',
        \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
    )->setComment(
        ' Question to aNswer Linkage Table'
    );
    $installer->getConnection()->createTable($table);

1 个答案:

答案 0 :(得分:1)

因为列类型不匹配。如我所见 question_id和answer_id在参考表中具有 TYPE_SMALLINT ,但在问题answer中,您创建的是 TYPE_INTEGER 。因此,要解决此问题,只需将 TYPE_INTEGER 更改为 TYPE_SMALLINT ,问题答案将如下所示:

$table = $installer->getConnection()->newTable(
            $installer->getTable('questionanswer')
        )->addColumn(
            'question_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['nullable' => false, 'primary' => true],
            'Question ID'
        )->addColumn(
            'answer_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['nullable' => false, 'primary' => true],
            'Answer ID'
        )->addIndex(
            $installer->getIdxName('questionanswer', ['answer_id']),
            ['answer_id']
        )->addForeignKey(
            $installer->getFkName('questionanswer', 'question_id', 'faqquestion', 'question_id'),
            'question_id',
            $installer->getTable('faqquestion'),
            'question_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->addForeignKey(
            $installer->getFkName('questionanswer', 'answer_id', 'faqanswer', 'answer_id'),
            'answer_id',
            $installer->getTable('faqanswer'),
            'answer_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->setComment(
            ' Question to aNswer Linkage Table'
        );

这将解决您的问题

致谢