spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

foreign key problem #159

Closed marcelloh closed 8 years ago

marcelloh commented 8 years ago

unclear how to define a foreign key that also can be null

I now get a mysql error: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails probably because there has to be a matching id, which is not always the case.

And I can't seem to find the options in the docs that can be used to configure an entity-field Part of the entity: 'user3_id' => ['type' => 'integer','index' => true],

I found in Manager some settings, so I played with the notnull, but both give me the same error:

            'user3_id'                  => ['type' => 'integer', 'notnull' => false, 'index' => true],
            'user3_id'                  => ['type' => 'integer', 'notnull' => true, 'index' => true],
nebulousGirl commented 8 years ago

Make sure the value put in user3_id exists as a primary key in your Users table. By default in Spot, you should be able to put null instead of a primary key in user3_id if you don't want to set the relationship.

You might have an empty string instead of null.

marcelloh commented 8 years ago

The NULL part did it for me :-) Thanks again

nebulousGirl commented 8 years ago

You should close the issue if your problem is solved.