Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table already exists error if there is intermediate entity for many-to-many relationship #102

Open
rela589n opened this issue Sep 22, 2020 · 2 comments

Comments

@rela589n
Copy link

rela589n commented Sep 22, 2020

I get next error when try to do pa doctrine:schema:update

The table with name 'new_ads_doctrine.proposals' already exists.
at vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema/SchemaException.php:112

Customer:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="App\Entities\Customer" table="customers">
        <id name="uuid" type="string" column="id">
            <generator strategy="NONE"/>
        </id>

        <embedded name="email" class="App\ValueObjects\Email" use-column-prefix="false"/>
        <embedded name="password" class="App\ValueObjects\Password" use-column-prefix="false"/>

        <one-to-many field="postedJobs" target-entity="App\Entities\Job" mapped-by="customer"/>
    </entity>
</doctrine-mapping>

Freelancer:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="App\Entities\Freelancer" table="freelancer">
        <id name="uuid" type="string" column="id">
            <generator strategy="NONE"/>
        </id>

        <embedded name="email" class="App\ValueObjects\Email" use-column-prefix="false"/>
        <embedded name="password" class="App\ValueObjects\Password" use-column-prefix="false"/>
        <embedded name="hourRate" class="App\ValueObjects\Money" use-column-prefix="false"/>

        <one-to-many field="proposals" target-entity="App\Entities\Proposal" mapped-by="freelancer"/>
    </entity>
</doctrine-mapping>

Job:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="App\Entities\Job" table="jobs">
        <id name="uuid" type="string" column="id">
            <generator strategy="NONE"/>
        </id>

        <embedded name="title" class="App\ValueObjects\JobTitle" use-column-prefix="false"/>
        <embedded name="description" class="App\ValueObjects\JobDescription" use-column-prefix="false"/>

        <many-to-one field="customer" target-entity="App\Entities\Customer" inversed-by="postedJobs"/>
        <one-to-many field="proposals" target-entity="App\Entities\Proposal" mapped-by="job"/>

        <many-to-many field="freelancersApplied" target-entity="App\Entities\Freelancer">
            <join-table name="proposals">
                <join-columns>
                    <join-column name="job_id"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="freelancer_id"/>
                </inverse-join-columns>
            </join-table>
        </many-to-many>
    </entity>
</doctrine-mapping>

Proposal:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="App\Entities\Proposal" table="proposals">
        <id name="uuid" type="string" column="id">
            <generator strategy="NONE"/>
        </id>

        <embedded name="coverLetter" class="App\ValueObjects\CoverLetter" use-column-prefix="false"/>
        <embedded name="estimatedTime" class="App\ValueObjects\EstimatedTime" use-column-prefix="false"/>

        <many-to-one field="job" target-entity="App\Entities\Job" inversed-by="proposals"/>
        <many-to-one field="freelancer" target-entity="App\Entities\Freelancer" inversed-by="proposals"/>
    </entity>
</doctrine-mapping>

Or is there way to explicitly define intermediate entity for many-to-many relationship?

@eigan
Copy link
Member

eigan commented Sep 23, 2020

Your first table, (many-to-many on freelancersApplied)

<join-table name="proposals">

Your second table

<entity name="App\Entities\Proposal" table="proposals">

I would suggest renaming your many-to-many table to something like proposals_freelancers.

- <join-table name="proposals">
+ <join-table name="proposals_freelancers">

@rela589n
Copy link
Author

But it should be the same table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants