Magento2 InstallSchema.php不会创建指定的表


13

我有一个InstallSchema.php,只是没有在数据库中创建必要的表。模式的代码如下所示:

<?php

namespace MyVendor\Helpdesk\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
*   @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface    $setup, 
                            ModuleContextInterface  $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $table = $installer->getConnection()
                            ->newTable($installer->getTable('myvendor_helpdesk_ticket'))
                            ->addColumn(
                                    'ticket_id',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                                    null,
                                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                                    'Ticket Id'
                            )
                            ->addColumn(
                                    'customer_id',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                                    null,
                                    ['unsigned' =>  true],
                                    'Customer Id'
                            )
                            ->addColumn(
                                    'title',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                                    null,
                                    ['nullable' =>  false],
                                    'Title'
                            )
                            ->addColumn(
                                    'severity',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                                    null,
                                    ['nullable' =>  false],
                                    'Severity'
                            )
                            ->addColumn(
                                    'created_at',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                                    null,
                                    ['nullable' =>  false],
                                    'Created At'
                            )
                            ->addColumn(
                                    'status',
                                    \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                                    null,
                                    ['nullable' =>  false],
                                    'Status'
                            )
                            ->addIndex(
                                    $installer->getIdxName('myvendor_helpdesk_ticket',  ['customer_id']),
                                    ['customer_id']
                            )
                            ->addForeignKey(
                                    $installer->getFkName('myvendor_helpdesk_ticket', 'customer_id', 'customer_entity', 'entity_id'),
                                    'customer_id',
                                    $installer->getTable('customer_entity'),
                                    'entity_id',
                                    \Magento\Framework\DB\Ddl\Table::ACTION_SET_NULL
                            )
                            ->setComment('myvendor  Helpdesk Ticket');
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

我运行了以下命令:

php bin/magento setup:upgrade
php bin/magento setup:db-schema:upgrade

没有错误,但是,当我进入我的页面时,我收到了休闲错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.myvendor_helpdesk_ticket' doesn't exist

请帮忙。


您是否在任何地方定义了“ myvendor_helpdesk_ticket”?它可能只是在installer-> getTable()上失败。
dbcn

您定义的意思是什么?可以请你提供一个小例子
Lachezar Raychev

我确实创建了一个ResourceModel,在其中执行app / code / MyVendor / Helpdesk / Model / ResourceModel / Ticket.php:$ this-> _ init('myvendor_helpdesk_ticket','ticket_id'); 如果是定义它...我发布的代码中其他所有包含'myvendor_helpdesk_ticket'字符串的地方
Lachezar Raychev 2016年

我用“-> newTable('myvendor_helpdesk_ticket')”替换了“-> newTable($ installer-> getTable('myvendor_helpdesk_ticket'))” ...仍然没有
Lachezar Raychev

3
尝试从表中删除setup_module的纪录module = 'MyVendor_Helpdesk'
马吕斯

Answers:


33

如果安装程序已经执行,则不会再次运行。
setup_module模块='MyVendor_Helpdesk'中删除记录应使其再次运行。


Marius很棒,对我有用。
Naveenbos

10

请尝试以下代码。

delete from setup_module where  module ='module_name';
sudo bin/magento setup:upgrade
sudo bin/magento setup:di:compile
sudo bin/magento cache:clean

如果在var文件夹上出现任何权限错误。

sudo chmod -R 777 var/*

1

我经常做的就是删除表(如果存在于InstallSchema.php上):

public function install(SchemaSetupInterface $setup, ModuleContextInterface $context){
    $setup->startSetup();
    // Drop table for development purpose
    $setup->getConnection()->dropTable($setup->getTable('my_custom_table')); ...

之后,请执行以下操作:

  1. 访问您的数据库
  2. 运行以下命令:DELETE FROM setup_module WHERE module ='module_name';
  3. 退出数据库
  4. 在您的Magento根文件夹中执行:php bin / magento setup:db-schema:upgrade
  5. 检查您的自定义表中是否一切正常。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.