doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.91k stars 2.51k forks source link

ERROR: index "primary" does not exist #6271

Closed tlorens closed 7 years ago

tlorens commented 7 years ago

Cannot for the life of me find where this DROP INDEX "primary"; is coming from. If I execute each query statement generated from the output of "doctrine:schema:update" everything goes just fine. Symfony 3.2 / Postgres 9.6.1 / Doctrine 2.5 (bundle 1.6)

  An exception occurred while executing 'DROP INDEX "primary"':
  SQLSTATE[42704]: Undefined object: 7 ERROR:  index "primary" does not exist

I've searched my code and there's not a single instance of declaring any indexes named primary.

Ocramius commented 7 years ago

This information is not sufficient to provide any help on our side. You'll have to debug further, maybe with a trace and dumping the schema diff object.

Closing as invalid

tlorens commented 7 years ago

Dug a little deeper into the issue. I may actually attempt to fix it. Seems like any time a primary key is changed (standard sequence, auto-increment to/from guid) it has an issue fetching the tables metadata (primary keys/index name). I'm going take a stab at this on my own and see if I can come up with the fix

johnpancoast commented 7 years ago

@Ocramius this is definitely a legit bug. It has seemed intermittent for me but I can reproduce it using postgres and a composite key.

/**

Ocramius commented 7 years ago

@johnpancoast can you put this in a test case? I think this might be a DBAL issue though.

johnpancoast commented 7 years ago

@Ocramius sure.

plimpton commented 7 years ago

Just came across this as well, postgresql trying to remove a composite key and replace it with a single id column because i needed to add a WHERE conditional to the composite key

Ocramius commented 7 years ago

@plimpton try making a test case and a new PR with just the failing scenario, if you can

jrysig commented 7 years ago

I can reproduce this problem too.

<?php
// bootstrap.php
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$isDevMode = true;

$paths = array(__DIR__.'/entity');
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);

$dbParams = array(
    'driver'   => 'pdo_pgsql',
    'user'     => 'doctrine_test',
    'password' => 'doctrine_test',
    'dbname'   => 'doctrine_test',
    'host'     => 'localhost',
    'charset'  => 'UTF8',
);

$entityManager = EntityManager::create($dbParams, $config);
<?php
// cli-config.php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

require_once 'bootstrap.php';

return ConsoleRunner::createHelperSet($entityManager);
<?php
// entity/user.php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
   private $sapid;
}
# composer.json
{
    "name": "john/doctrine_test",
    "require": {
        "doctrine/orm": "*"
    }
}

(create postgresql user and database, see dbParams)

composer install vendor/bin/doctrine orm:schema-tool:update --force

Updating database schema... Database schema updated successfully! "2" queries were executed

(rename primary key sapid to something else) vendor/bin/doctrine orm:schema-tool:update --force

Updating database schema... [Doctrine\DBAL\Exception\DriverException]
An exception occurred while executing 'DROP INDEX "primary"':
SQLSTATE[42704]: Undefined object: 7 ERROR: index "primary" does not exist

vendor/bin/doctrine orm:schema-tool:update --dump-sql

DROP INDEX "primary"; ALTER TABLE "User" RENAME COLUMN sapid TO sapid2; ALTER TABLE "User" ADD PRIMARY KEY (sapid2);

anacicconi commented 7 years ago

I have the same bug using Doctrine + Oracle when I'm changing a composite key.

jrysig commented 7 years ago

@Ocramius, could you please re-open this issue?

Ocramius commented 7 years ago

As asked already twice, a test case is needed. The above are examples.

johnpancoast commented 7 years ago

Apologies for not getting to writing this test case, but I have no time at the moment due to other projects (typical story, I know). It's easily re-produceable using those steps iirc. That's the most I can offer at the moment, unfortunately.

simPod commented 6 years ago

I think this is being resolved in https://github.com/doctrine/dbal/pull/2929

simobrazz commented 5 years ago

Any update on this? I'm struggling with this problem.

tchafack commented 5 years ago

Any update on this? I'm struggling with this problem.

Hello, I had the same problem; I just drop manually the of the entity (database table) you need to change indexes (for example DROP INDEX actual_pk) and then re-run your doctrine update or query.

goetas commented 4 years ago

Most probably related to https://github.com/doctrine/dbal/pull/3936

tlorens commented 4 years ago

As asked already twice, a test case is needed. The above are examples.

Eh, example would be fresh install of a Symfony project using Postgres. Nothing special needs to be done. It just happens out of the box. Don't even need to drop any indexes. Just make an entity change, generate a migration and there it goes.

goetas commented 4 years ago

https://github.com/doctrine/dbal/pull/3936 is solving the issue (builds were green and tests oo), but is rather a big change and probably should be split in 2 or 3 different pull request. Anyone willing to help?

f-lombardo commented 1 year ago

The problem is still present (2.14.1). Is there any way to solve it? Thanks in advance.

johnpancoast commented 1 year ago

@f-lombardo, maybe check out @tlorens' workaround above.

Until one of us writes a simple test case, the contributors cannot do much. I was super busy when I wrote this and didn't have time to go through doctrine's merge standards and all that at the time. I can't commit time for certain (I'm an awful human) but I'll try to do it unless somebody beats me to it. The test itself should be easy, and can likely just do something similar to this (just my assumption). Once that's written contributors can dig in.

f-lombardo commented 1 year ago

Thank you @johnpancoast , see https://github.com/doctrine/dbal/pull/6025

johnpancoast commented 1 year ago

Glad I could help... well, at least like .5% like 5 years ago :)

f-lombardo commented 1 year ago

Fixed: https://github.com/doctrine/dbal/pull/6025