Database 未执行Magento 2 InstallSchema方法

Database 未执行Magento 2 InstallSchema方法,database,magento,module,magento2,Database,Magento,Module,Magento2,在我自己的Magento 2自定义模块上,我想安装一个自定义数据库表。这是InstallSchema类代码: <?php namespace MyVendor\MyModule\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterfa

在我自己的Magento 2自定义模块上,我想安装一个自定义数据库表。这是InstallSchema类代码:

<?php
namespace MyVendor\MyModule\Setup;

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

class InstallSchema implements InstallSchemaInterface
{
    /**
     * @inheritdoc
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();


            $table = $setup->getConnection()
                ->newTable($setup->getTable('my_table'))
                ->addColumn(
                    'greeting_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Greeting ID'
                )
                ->addColumn(
                    'message',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable' => false, 'default' => ''],
                    'Message'
                )->setComment("Greeting Message table");

            $setup->getConnection()->createTable($table);

        $setup->endSetup();
    }
}

请在InstallSchema文件中声明并添加依赖项,如下所示:-

namespace <Your_Package>\<Your_Module>\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
namespace\\Setup;
使用Magento\Framework\Setup\InstallSchemaInterface;
使用Magento\Framework\Setup\SchemaSetupInterface;
使用Magento\Framework\Setup\ModuleContext接口;
然后清除缓存,从生成的文件夹中删除数据,并从安装模块中删除模块条目。
确保在module.xml文件中定义了正确的安装版本,并找到了解决方案。它是module.xml文件中的模块名。setup:upgrade安装程序在区分大小写的文件夹路径中搜索InstallSchema,因此如果您将模块声明为

my_模块

它将搜索MyVendor/my/module/Setup文件夹

我通过将名称替换为以下内容来解决此问题:

MyModule

因此,安装程序将在MyVendor/MyModule/Setup文件夹中正确搜索


通过将日志消息放在Magento安装文件夹的Install.php文件中,我发现了“旧方法”的问题。

@wildchild使用其他解决方案很好。在您的问题中,您没有提到您使用的是2.2.*还是2.3.*,您只是说明了Magento 2

在从一台服务器移动到另一台服务器并从2.2升级到2.3之后,我的自定义模块停止工作,PHP脚本没有在数据库中创建表,我也遇到了同样的问题

我后来发现Magento已经改变了音乐,现在你必须使用声明性模式 模块供应商/Module\u名称/etc/db\u schema.xml
文件声明模块的数据库结构。

请参见下面的示例,您应该

创建一个表

下面的示例创建具有四列的声明性_表。id_列是主键

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="declarative_table">
        <column xsi:type="int" name="id_column" padding="10" unsigned="true" nullable="false" comment="Entity Id"/>
       <column xsi:type="int" name="severity" padding="10" unsigned="true" nullable="false" comment="Severity code"/>
        <column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
       <column xsi:type="timestamp" name="time_occurred" padding="10" comment="Time of event"/>
       <constraint xsi:type="primary" referenceId="PRIMARY">
           <column name="id_column"/>
       </constraint>
   </table>
</schema>

在我的例子中,
包名
是错误的。用正确的包名替换后,问题得到解决

namespace Package_Name\Module_Name\Setup;

不走运,我已经在类中完成了正确的名称空间和使用。我删除了生成的文件夹内容并清除了缓存。同时删除数据库表中的模块行。尝试了所有操作。您是否在module.xmlYes@Andrew中添加了安装版本?我已经添加了:(这是我在setup\u module表中看到的正确版本)似乎由于某种原因从未调用过安装方法。您是否启用了模块
bin/magento模块:启用MyVendor\u MyModule
从“setup\u module”表中删除模块条目,并删除模块表单config.php文件。再次尝试启用您的模块。安装模块时将创建magento 2表。模块已启用。尝试删除config.php上的条目,删除数据库上的setup\u模块记录,然后安装:升级,什么都不起作用。还尝试创建一个新模块,同样。我猜InstallSchema代码或模块的文件夹结构可能有问题。InstallSchema文件位于MyVendor/MyModule/Setup文件夹下。
namespace Package_Name\Module_Name\Setup;