5

My module was implemented and working on Magento 2.2.

We are moving to Magento 2.3, but failed in Declarative Schema.

I follow this. Created db_schema.xml. Following is the example

    <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">

   <table name="options" resource="default" engine="innodb" comment="Option entities">
        <column xsi:type="int" name="option_id" padding="10" unsigned="true" identity="true" nullable="false" comment="Option Id"/>
        <column xsi:type="varchar" name="name" nullable="false" length="255" comment="Option name"/>
        <column xsi:type="varchar" name="value" nullable="false" length="255" comment="Option value"/>
        <column xsi:type="timestamp" name="created" comment="Time of event"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="option_id"/>
        </constraint>
    </table>
</schema>

Then, whatever commands I tried to run returns the same error..

PHP Fatal error:  Uncaught TypeError: Return value of Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder::processTable() must be an instance of Magento\Framework\Setup\Declaration\Schema\Dto\Table, boolean returned in C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php:251
Stack trace:
#0 C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php(148): Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder->processTable(Object(Magento\Framework\Setup\Declaration\Schema\Dto\Schema), Array)
#1 C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\SchemaConfig.php(78): Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder->build(Object(Magento\Framework\Setup\Declaration\Schema\Dto\Schema))
#2 C:\xampp\htdocs\magento23\setup\src\Magento\Setup\Model\DeclarationInstaller.php(57): Magento\Framework\Setup\Declaration\Schema\SchemaConfig->getDeclarationConfig()
#3 C:\ in C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php on line 251

Fatal error: Uncaught TypeError: Return value of Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder::processTable() must be an instance of Magento\Framework\Setup\Declaration\Schema\Dto\Table, boolean returned in C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php:251
Stack trace:
#0 C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php(148): Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder->processTable(Object(Magento\Framework\Setup\Declaration\Schema\Dto\Schema), Array)
#1 C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\SchemaConfig.php(78): Magento\Framework\Setup\Declaration\Schema\Declaration\SchemaBuilder->build(Object(Magento\Framework\Setup\Declaration\Schema\Dto\Schema))
#2 C:\xampp\htdocs\magento23\setup\src\Magento\Setup\Model\DeclarationInstaller.php(57): Magento\Framework\Setup\Declaration\Schema\SchemaConfig->getDeclarationConfig()
#3 C:\ in C:\xampp\htdocs\magento23\vendor\magento\framework\Setup\Declaration\Schema\Declaration\SchemaBuilder.php on line 251

Commands which produces the above error:

bin/magento setup:db-declaration:generate-whitelist   
bin/magento module:upgrade

I tried to install the module on a fresh installed Magento 2.3

Can anyone please help?

1
  • 1
    Please check my ans and let me know if you have any issue. Commented Feb 7, 2019 at 5:25

2 Answers 2

19

I had the exact same issue and discovered it was caused in a custom module. The problem was that setAreaCode() was called in the constructor. I relocated setAreaCode() into the execute() method and the issue was fixed.

8
  • 1
    Seems like any php or Magento functions can cause this output error. So I traced all the module code to find the bugs. The error did not even related to the declarative schema... After fix the error, upgrade can run.
    – BlueTong
    Commented Feb 13, 2019 at 7:15
  • @BlueTong Could you tell us more about the bug you found that causes this? I also ran into this problem while following Alan Storm's tutorial on Object Repositories and the solution in this answer made the error go away. Commented Sep 16, 2019 at 19:19
  • 1
    Excellent! You saved my time! A lot of hours! Thank you! The solution is works. Commented Jun 5, 2020 at 11:43
  • Thank you a lot! I had the same error and I waste time searching for something related to Declarative Schema. I changed the setAreaCode() from constructor to the execute and instantly it worked. Commented Aug 28, 2020 at 17:21
  • This saved my day, cannot thank enough.
    – Wolfack
    Commented Sep 8, 2020 at 9:07
0

Check MySQL name length limits (table name/columns/indexes etc)

In my case I got this exact error, and not at all related to setAreaCode() as original accepted answer, for me it was a custom module that had entries in etc/db_schema.xml defined which are table names that were longer than the MySQL permitted table name length:

The maximum length of a table name is 64 characters long according to MySQL v8.0.12. (MySQL version 7.3x as well)

Also according to StackOverflow question/answer it applies to most types:

Names for databases, tables, columns, and indexes can be up to 64 characters long. Alias names can be up to 256 characters long.

I got rid of the clearly broken (incomplete) custom module, but before getting rid of it I tested by shortening the table names, and then Magento 2 could install the module.

How to Debug this error for your case

As mentioned by @BlueTong, this error can be thrown anywhere, it might not even be related to your db_schema.xml; it is clear that Magento 2.x (2.3 in my case) do not handle the Exception better, if they added more information it would have been clearer as to the origin of the error.

This error causes /magento/framework/DB/Adapter/Pdo/MySQL.php to rollback any transactions:

    /**
     * Check if all transactions have been committed
     *
     * @return void
     */
    public function __destruct()
    {
        if ($this->_transactionLevel > 0) {
            trigger_error('Some transactions have not been committed or rolled back', E_USER_ERROR);
        }
    }

The best place to start debugging is /vendor/magento/framework/Setup/Declaration/Schema/Declaration/SchemaBuilder.php at function processTable. This is where *all the database tables are processed, so it might take a while to find the relevant one that causes issues.

Add a try..catch before exiting the function (include getTableName() as well).

Once you have the table causing issues, add conditional breakpoints for custom table names causing problems to continue debugging again.

array $tableData 

.... put a conditional breakpoint on $tableData['name'], 
     for instance : 

strpos($tableData['name'], 'tablename') !== false

Inspect the information, for me it was rather easy once I saw the long table name, I assumed it might be the cause as it looked very long and confirmed name limit in MySQL.

Remember, this error is so generic; that it is caused by any code, so the cause may not be as easy to find, but this may be a starting point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.