I had the same error after adding a prefix (via phpmyadmin) to the table sequence_product
and copying the table from another magento database into my database.
Problem: The prefixed table was still used instead of the copied one, so I had to find out which table is still using the prefixed table instead of the copied one.
I had to debug the file vendor/magento/framework/Setup/Declaration/Schema/Db/SchemaBuilder.php to add some debug output:
private function processReferenceKeys(array $tables, Schema $schema)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/zend_debug.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
foreach ($tables as $table) {
$logger->info("Table: " . $table->getName());
$tableName = $table->getName();
if ($schema->getTableByName($tableName) instanceof Table) {
continue;
}
$referencesData = $this->dbSchemaReader->readReferences($tableName, $table->getResource());
$references = [];
foreach ($referencesData as $referenceData) {
//Prepare reference data
$referenceData['table'] = $table;
$referenceTableName = $referenceData['referenceTable'];
$logger->info("Ref-Table: " . $referenceTableName);
$referenceData['column'] = $table->getColumnByName($referenceData['column']);
$referenceData['referenceTable'] = $this->tables[$referenceTableName];
$referenceData['referenceColumn'] = $referenceData['referenceTable']->getColumnByName(
$referenceData['referenceColumn']
);
$references[$referenceData['name']] = $this->elementFactory->create('foreign', $referenceData);
//We need to instantiate tables in order of references tree
if (isset($tables[$referenceTableName]) && $referenceTableName !== $tableName) {
$this->processReferenceKeys([$referenceTableName => $tables[$referenceTableName]], $schema);
unset($tables[$referenceTableName]);
}
}
$table->addConstraints($references);
$schema->addTable($table);
}
}
Now run bin setup:upgrade
again and look at the logfile /var/log/zend_debug.log.
Look at the last entries, you will see the table and the ref table. In my case it was the table email_catalog
.
Now open the table in phpmyadmin, click on the tab structure then on relation view and select the correct table
What you should learn from this:
Never prefix a table to backup it! Instead create a new database and copy the tables you try to backup into it.