Dbal: SchemaDiff throws `SchemaException("There is no column ....")` since v2.5.5

Created on 10 Sep 2016  路  5Comments  路  Source: doctrine/dbal

code:

<?php
require_once __DIR__ . '/vendor/autoload.php';

$common = array(
    'driver'   => 'pdo_mysql',
    'host'     => '127.0.0.1',
    'user'     => 'user',
    'password' => 'pass',
);

// from Connection
$fromParams = $common;
$fromParams['dbname'] = 'test1';
$fromConn = \Doctrine\DBAL\DriverManager::getConnection($fromParams);

// to Connection
$toParams = $common;
$toParams['dbname'] = 'test2';
$toConn = \Doctrine\DBAL\DriverManager::getConnection($toParams);

// create dummy table
$fromConn->exec('
CREATE TABLE IF NOT EXISTS t_table (
  pid int(11) NOT NULL,
  PRIMARY KEY (pid)
)');
$toConn->exec('
CREATE TABLE IF NOT EXISTS t_table (
  sid int(11) NOT NULL,
  PRIMARY KEY (sid)
)');

// print sql diff
$fromSchema = $fromConn->getSchemaManager()->createSchema();
$toSchema = $toConn->getSchemaManager()->createSchema();
$diff = \Doctrine\DBAL\Schema\Comparator::compareSchemas($fromSchema, $toSchema);
print_r($diff->toSql($fromConn->getDatabasePlatform()));

v2.5.4 is

[0] => ALTER TABLE t_table DROP PRIMARY KEY
[1] => ALTER TABLE t_table CHANGE pid sid INT NOT NULL
[2] => ALTER TABLE t_table ADD PRIMARY KEY (sid)

but v2.5.5 is

Fatal error: Uncaught exception 'Doctrine\DBAL\Schema\SchemaException' with message 'There is no column with name 'sid' on table 't_table'.' in /path/lib/Doctrine/DBAL/Schema/SchemaException.php on line 86

I would like to append to a "MySqlPlatform.php:702" like a below.

if (!$diff->fromTable->hasColumn($columnName)) {
    continue;
}

refs #2302

Bug

Most helpful comment

Same issue here with an oracle database.

Warning: oci_fetch_all(): ORA-01427: single-row subquery returns more than one row in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php on line 290

[Doctrine\DBAL\Schema\SchemaException]
There is no column with name '....' on table '...'.

Since 2.5.5, this query fails:

SELECT c.*,
  (SELECT d.comments
  FROM all_col_comments d
  WHERE d.TABLE_NAME = c.TABLE_NAME
  AND d.COLUMN_NAME  = c.COLUMN_NAME
  ) AS comments
FROM all_tab_columns c
WHERE c.table_name = 'MY_TABLE'
AND c.owner        = 'THE_OWNER'
ORDER BY c.column_name;

On 2.5.4, the query was with INNER JOIN:

SELECT c.*,
  d.comments
FROM all_tab_columns c
INNER JOIN all_col_comments d
ON d.TABLE_NAME    = c.TABLE_NAME
AND d.COLUMN_NAME  = c.COLUMN_NAME
WHERE c.table_name = 'MY_TABLE'
AND c.owner        = 'THE_OWNER'
ORDER BY c.column_name;

Reverting back to 2.5.4 solve the issue.

All 5 comments

Same issue here with an oracle database.

Warning: oci_fetch_all(): ORA-01427: single-row subquery returns more than one row in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php on line 290

[Doctrine\DBAL\Schema\SchemaException]
There is no column with name '....' on table '...'.

Since 2.5.5, this query fails:

SELECT c.*,
  (SELECT d.comments
  FROM all_col_comments d
  WHERE d.TABLE_NAME = c.TABLE_NAME
  AND d.COLUMN_NAME  = c.COLUMN_NAME
  ) AS comments
FROM all_tab_columns c
WHERE c.table_name = 'MY_TABLE'
AND c.owner        = 'THE_OWNER'
ORDER BY c.column_name;

On 2.5.4, the query was with INNER JOIN:

SELECT c.*,
  d.comments
FROM all_tab_columns c
INNER JOIN all_col_comments d
ON d.TABLE_NAME    = c.TABLE_NAME
AND d.COLUMN_NAME  = c.COLUMN_NAME
WHERE c.table_name = 'MY_TABLE'
AND c.owner        = 'THE_OWNER'
ORDER BY c.column_name;

Reverting back to 2.5.4 solve the issue.

Same here using doctrine:migrations:diff. Every @ManyToMany Join Table generates:

[Doctrine\DBAL\Schema\SchemaException]
There is no column with name 'id' on table '...'.

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Contract", inversedBy="expectedFaults")
     * @ORM\JoinTable(name="expected_faults",
     *      joinColumns={@ORM\JoinColumn(name="fault_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="contract_id", referencedColumnName="id")}
     *      )
     */

I can confirm this. In fact, we have a number of users that have been hitting this in our ManyToMany tutorial (https://knpuniversity.com/screencast/collections/many-to-many-extra-fields#comment-3094129612), so it's definitely legit (but I know these things are complex!).

I hit the bug too, and can confirm that the fix provided by @arima-ryunosuke works in my case, which is adding new columsn to the table and then recreate the primary key to use the old columns plus the new ones.

This issue is fixed at v2.5.13 by #2696

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomasPilar picture TomasPilar  路  5Comments

corphi picture corphi  路  7Comments

DuckThom picture DuckThom  路  6Comments

doctrinebot picture doctrinebot  路  7Comments

photodude picture photodude  路  5Comments