【问题标题】:Magento 2.3 setup “InstallSchema” script not creating tableMagento 2.3 设置“InstallSchema”脚本未创建表
【发布时间】:2019-09-04 16:51:16
【问题描述】:

我已使用安装程序创建表,但脚本未创建表。我还从 setup_module 表中删除了该条目,但仍然没有用。我尝试使用bin/magento setup:install --convert-old-scripts=1 迁移到声明性架构,因为它是Magento 2.3 安装,但原始脚本需要先运行。

magento-path/app/code/Folder/CustomModule/Setup/InstallSchema.php

<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Folder\CustomModule\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
    * {@inheritdoc}
    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
    */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
          /**
          * Create table 'shop_ocxe'
          */
          $setup->startSetup();

          $table = $setup->getConnection()
              ->newTable($setup->getTable('shop_ocxe'))
              ->addColumn(
                  'id',
                  \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                  null,
                  ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                  'Shop ID'
              )
              ->addColumn(
                  'url',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  ['nullable' => false],
                  'Url'
              )
              ->addColumn(
                  'name',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  ['nullable' => false],
                  'Name'
              )
              ->addColumn(
                  'phrase',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  [],
                  'Phrase'
              )
              ->addColumn(
                  'logo',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  [],
                  'Logo'
              )
              ->addColumn(
                  'banner',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  [],
                  'Banner'
              )
              ->addColumn(
                  'author',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  ['nullable' => false],
                  'Author'
              )
              ->addColumn(
                  'author_photo',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  255,
                  ['nullable' => false],
                    'Author Photo'
              )
              ->addColumn(
                  'desc',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                  '64k',
                  [],
                  'Desc'
              )
              ->addColumn(
                  'status',
                  \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                  null,
                  ['nullable' => false, 'default' => 1],
                  'Status'
              )
              ->addColumn(
                  'created_at',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                  null,
                  ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
                  'Created At'
              )->addColumn(
                  'updated_at',
                  \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                  null,
                  ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT_UPDATE],
                  'Updated At'
              )->setComment("Shop Table");
          $setup->getConnection()->createTable($table);

          $setup->endSetup();
      }
}

【问题讨论】:

    标签: magento schema magento2 magento-2.3


    【解决方案1】:

    从脚本更新到 declarative schema 对我有用。对于任何使用 Magento 2.3 及更高版本的人来说,这似乎是必须的。

    表格方案示例:

    <table name="catalog_product_entity_datetime" resource="default" engine="innodb"
               comment="Catalog Product Datetime Attribute Backend Table">
        <column xsi:type="int" name="value_id" padding="11" unsigned="false" nullable="false" identity="true" comment="Value ID"/>
        <column xsi:type="smallint" name="attribute_id" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Attribute ID"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="false" identity="false" default="0" comment="Store ID"/>
        <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="false" default="0" comment="Entity ID"/>
        <column xsi:type="datetime" name="value" on_update="false" nullable="true" comment="Value"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="value_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="CAT_PRD_ENTT_DTIME_ATTR_ID_EAV_ATTR_ATTR_ID" table="catalog_product_entity_datetime" column="attribute_id" referenceTable="eav_attribute" referenceColumn="attribute_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="CAT_PRD_ENTT_DTIME_ENTT_ID_CAT_PRD_ENTT_ENTT_ID" table="catalog_product_entity_datetime" column="entity_id" referenceTable="catalog_product_entity" referenceColumn="entity_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="CATALOG_PRODUCT_ENTITY_DATETIME_STORE_ID_STORE_STORE_ID" table="catalog_product_entity_datetime" column="store_id" referenceTable="store" referenceColumn="store_id" onDelete="CASCADE"/>
        <constraint xsi:type="unique" referenceId="CATALOG_PRODUCT_ENTITY_DATETIME_ENTITY_ID_ATTRIBUTE_ID_STORE_ID">
            <column name="entity_id"/>
            <column name="attribute_id"/>
            <column name="store_id"/>
        </constraint>
        <index referenceId="CATALOG_PRODUCT_ENTITY_DATETIME_ATTRIBUTE_ID" indexType="btree">
            <column name="attribute_id"/>
        </index>
        <index referenceId="CATALOG_PRODUCT_ENTITY_DATETIME_STORE_ID" indexType="btree">
            <column name="store_id"/>
        </index>
    </table>
    

    查看更多:Magento docs

    【讨论】:

      【解决方案2】:

      试试下面的步骤:

       1. delete Jk_Ocxe entry from setup_module table .
       2. php bin/magento setup:upgrade
      

      我已经尝试过,并且正在我的系统上创建表。

      【讨论】:

      • 谢谢,我做了上面的步骤,但似乎是版本兼容性问题而不是脚本问题。 Magento 2.3 出于某种原因忽略了我的脚本。
      • @James : 你在运行 upgrade cmd 时遇到任何错误吗?
      • 不是真的,这有点令人沮丧。我看到某人的帖子有同样的问题(更糟糕的是 - 自定义模块从 2.2 到 2.3 停止工作)并且只有更新到声明性模式才有效。对我来说幸运的是,它也成功了。
      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多