0

Is it possible to have two tables with the same name in different schema in MSSQL?

For example dbo.tableX dbo2.tableX ?

If I try it in console doctrine:schema:validate, I get exception "Table with name DB.tableX already exists". It looks as if Doctrine is ignoring database schema.

Each entity is declared by annotation like "@ORM\Table(name="tableX", schema="dbo")". I also try "@ORM\Table(name="dbo.TableX")", but without success

In production it runs fine, but I cannot build new entities from database...

  • are you using 2 different entity manager? – goto Feb 27 '18 at 08:35
  • It runs fine in Symfony, but i have problem only when generating entities in console - doctrine:schema:validate generate, etc... – Filip Štencl Feb 27 '18 at 08:47
  • @FilipŠtencl: Have you tried explicitely selecting an entity manager with the `--em` option? – lxg Feb 27 '18 at 08:48
  • I didnt know it. I am trying now command doctrine:schema:validate --em "default"(because I dont have other) and i get the same error... "default" is string in config.yml in orm param – Filip Štencl Feb 27 '18 at 09:19

2 Answers2

0

If you want to have separate schema, you have 2 configure different entityManager as defined here: https://symfony.com/doc/current/doctrine/multiple_entity_managers.html

doctrine:
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    Main:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Main'
                        prefix: 'App\Entity\Main'
                        alias: Main
            customer:
                connection: customer
                mappings:
                    Customer:
                        is_bundle: false
                        type: annotation
                        dir: '%kernel.project_dir%/src/Entity/Customer'
                        prefix: 'App\Entity\Customer'
                        alias: Customer

Note that it's easier to separate your entity betweens 2 different bundles. (If you are using 2 different schema your entities are most likely very independant)

goto
  • 7,908
  • 10
  • 48
  • 58
-1

This must be a limitation in doctrine.

You can successfully try this with MS SQL Management Studio:

create table dbo.MyTable (t1 int not null primary key);
create schema dbo2;
create table dbo2.MyTable (t1 int not null primary key);

So yes, it's absolutely possible to have two (distinct) tables with the same names in separate schemas.

KarloX
  • 735
  • 8
  • 25
  • Yes, I know that it this is possible to have two tables in MSSQL. It must be some Doctrine limitation as you wrote. I use Symfony 2.7 with doctrine 2.5 – Filip Štencl Feb 27 '18 at 08:43
  • @Filip: Please re-read your own post. Your question was "Is it possible to have two tables with the same name in different schema in MSSQL?" And so I took the time to answer that question, and you honor that by voting my answer down and say you knew that. Thanks! – KarloX Feb 27 '18 at 09:12