DDC-2788: Create Table If Not Exists - doctrine:schema:update #3485

Closed
opened 2026-01-22 14:20:46 +01:00 by admin · 9 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 11, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user jayem:

I am not positive if this issue is in the correct project. Sorry if I placed it in the wrong area.

I was wondering if it would be possible to have the doctrine:schema:update command updated to use CREATE IF NOT EXISTS for tables that already exist.

For example, here is my setup:

    /****
     * The relationship between an author and its associated book entities.
     *
     * @ORM\ManyToMany(targetEntity="Book")
     * @ORM\JoinTable(name="authors_books",
     *     joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")}
     * )
     */
    protected $books;

The above code is an example, so the exact column names may not be 100% correct. The import aspect is the join table name. If that table already exists in the database, I will get the following error when I run the doctrine:schema:update --dump-sql command:

{quote}
[Doctrine\DBAL\Schema\SchemaException]
The table with the name 'authors_books' already exists.
{quote}

This is because it is trying to CREATE TABLE 'authors*books' and that table is already in the database. Could the command instead use CREATE TABLE IF NOT EXISTS 'authors*books' when "IF NOT EXISTS" is available for the configured database type?

Thanks!

Originally created by @doctrinebot on GitHub (Nov 11, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user jayem: I am not positive if this issue is in the correct project. Sorry if I placed it in the wrong area. I was wondering if it would be possible to have the `doctrine:schema:update` command updated to use `CREATE IF NOT EXISTS` for tables that already exist. For example, here is my setup: ``` /**** * The relationship between an author and its associated book entities. * * @ORM\ManyToMany(targetEntity="Book") * @ORM\JoinTable(name="authors_books", * joinColumns={@ORM\JoinColumn(name="book_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="author_id", referencedColumnName="id")} * ) */ protected $books; ``` The above code is an example, so the exact column names may not be 100% correct. The import aspect is the join table name. If that table already exists in the database, I will get the following error when I run the `doctrine:schema:update --dump-sql` command: {quote} `[Doctrine\DBAL\Schema\SchemaException]` `The table with the name 'authors_books' already exists.` {quote} This is because it is trying to `CREATE TABLE 'authors*books'` and that table is already in the database. Could the command instead use `CREATE TABLE IF NOT EXISTS 'authors*books'` when "`IF NOT EXISTS`" is available for the configured database type? Thanks!
admin added the ImprovementWon't Fix labels 2026-01-22 14:20:46 +01:00
admin closed this issue 2026-01-22 14:20:46 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 25, 2015):

Comment created by finalcode:

you can reference this question, http://stackoverflow.com/questions/3220998/check-for-table-existence-before-dropping

@doctrinebot commented on GitHub (May 25, 2015): Comment created by finalcode: you can reference this question, http://stackoverflow.com/questions/3220998/check-for-table-existence-before-dropping
Author
Owner

@e-onux commented on GitHub (Sep 29, 2016):

Problem is continuing since 2013? We must resolve this issue.

@e-onux commented on GitHub (Sep 29, 2016): Problem is continuing since 2013? We must resolve this issue.
Author
Owner

@Ocramius commented on GitHub (Sep 29, 2016):

Didn't notice this issue before. This is a no-go.

If the table exists, yet doctrine fails to recognize that, then there is some sort of issue with the setup.

Doctrine should just produce a CREATE TABLE and cause a hard failure here. Using CREATE TABLE IF NOT EXISTS (or equivalent - note that DB2 has no such construct!) just means introducing silent failures everywhere.

Closing as won't fix.

@Ocramius commented on GitHub (Sep 29, 2016): Didn't notice this issue before. This is a no-go. If the table exists, yet doctrine fails to recognize that, then there is some sort of issue with the setup. Doctrine should just produce a `CREATE TABLE` and cause a hard failure here. Using `CREATE TABLE IF NOT EXISTS` (or equivalent - note that DB2 has no such construct!) just means introducing silent failures everywhere. Closing as `won't fix`.
Author
Owner

@e-onux commented on GitHub (Sep 29, 2016):

https://github.com/doctrine/doctrine2/issues/6016
Issue is continuing. We didn't use many-to-many with latest version of doctrine.
We get this error on three tables like book, author, book_author.

@e-onux commented on GitHub (Sep 29, 2016): https://github.com/doctrine/doctrine2/issues/6016 Issue is continuing. We didn't use many-to-many with latest version of doctrine. We get this error on three tables like book, author, book_author.
Author
Owner

@Ocramius commented on GitHub (Sep 29, 2016):

That's a symptom of a different problem. Fixing it via IF NOT EXISTS would just be shotgun surgery. The source problem is that doctrine doesn't see your tables - find out the "why" first :-)

@Ocramius commented on GitHub (Sep 29, 2016): That's a symptom of a different problem. Fixing it via `IF NOT EXISTS` would just be shotgun surgery. The source problem is that doctrine doesn't see your tables - find out the "why" first :-)
Author
Owner

@e-onux commented on GitHub (Sep 29, 2016):

Thank you for your fast reply. You are saying this problem is not general, and only related for my codes/tables?
For hours I'm inspecting around /lib/Doctrine/DBAL/Schema/Schema.php
to resolve this.

@e-onux commented on GitHub (Sep 29, 2016): Thank you for your fast reply. You are saying this problem is not general, and only related for my codes/tables? For hours I'm inspecting around /lib/Doctrine/DBAL/Schema/Schema.php to resolve this.
Author
Owner

@Ocramius commented on GitHub (Sep 29, 2016):

If the table exists, yet doctrine still tries to create it, then:

  • doctrine probably didn't see the table
  • it's on a different schema
  • it is not visible to the currently connected user
  • doctrine may have recognized it with an incorrect name

And yes, this requires some manual debugging, sorry :-\

@Ocramius commented on GitHub (Sep 29, 2016): If the table exists, yet doctrine still tries to create it, then: - doctrine probably didn't see the table - it's on a different schema - it is not visible to the currently connected user - doctrine may have recognized it with an incorrect name And yes, this requires some manual debugging, sorry :-\
Author
Owner

@e-onux commented on GitHub (Sep 29, 2016):

This error only when I using many-to-many relation. I think this was about association mapping engine of doctrine. How do i profile this error on doctrine:schema:update command?

@e-onux commented on GitHub (Sep 29, 2016): This error only when I using many-to-many relation. I think this was about association mapping engine of doctrine. How do i profile this error on doctrine:schema:update command?
Author
Owner

@Ocramius commented on GitHub (Sep 29, 2016):

@e-onux fire up a debugger, and look at what is going on in cd11723e63/lib/Doctrine/ORM/Tools/SchemaTool.php (L854)

@Ocramius commented on GitHub (Sep 29, 2016): @e-onux fire up a debugger, and look at what is going on in https://github.com/doctrine/doctrine2/blob/cd11723e6310b5973f50e7d9819c58916721d290/lib/Doctrine/ORM/Tools/SchemaTool.php#L854
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3485