laravel-doctrine / migrations

Integration with Doctrine2's migrations package for Laravel
MIT License
75 stars 77 forks source link

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

Open rela589n opened 4 years ago

rela589n commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

But it should be the same table