DDC-746: ManyToOne: the generated schema does not enforce NOT NULL on the foreign key by default #918

Closed
opened 2026-01-22 12:55:33 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 13, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user djones:

In a @ManyToOne annotation, unless a @JoinColumn annotation with nullable=false is present, the generated schema has DEFAULT NULL rather than the expected NOT NULL for the foreign key.

class Person
{
    // ...

   /****
     * @Column(type="string")
     */
    private $name;

    /****
     * @OneToMany(targetEntity="Phone", mappedBy="person")
     */
    private $phones;

    // ...

    public function addPhone(Phone $phone)
    {
        if (!$this->phones->contains($phone)) {
            $this->phones->add($phone);
            $phone->setPerson($this);
        }
        return $this;
    }
}
class Phone
{
    // ...

    /****
     * @Column(type="string")
     */
    private $number;

    /****
     * @ManyToOne(targetEntity="Person", inversedBy="phones")
     */
    private $person;

    // ...

    public function setPerson(Person $person)
    {
        if ($this->person !== $person) {
            $this->person = $person;
            $person->addPhone($this);
        }
    }
}
## create-schema --dump-sql
Without the @JoinColumn:
CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL);
CREATE TABLE phones (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, person_id INTEGER DEFAULT NULL, number VARCHAR(255) NOT NULL)

With @JoinColumn(name="person_id", referencedColumnName="id", nullable=false) :
CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL);
CREATE TABLE phones (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, person_id INTEGER NOT NULL, number VARCHAR(255) NOT NULL)
Originally created by @doctrinebot on GitHub (Aug 13, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user djones: In a @ManyToOne annotation, unless a @JoinColumn annotation with nullable=false is present, the generated schema has DEFAULT NULL rather than the expected NOT NULL for the foreign key. ``` class Person { // ... /**** * @Column(type="string") */ private $name; /**** * @OneToMany(targetEntity="Phone", mappedBy="person") */ private $phones; // ... public function addPhone(Phone $phone) { if (!$this->phones->contains($phone)) { $this->phones->add($phone); $phone->setPerson($this); } return $this; } } ``` ``` class Phone { // ... /**** * @Column(type="string") */ private $number; /**** * @ManyToOne(targetEntity="Person", inversedBy="phones") */ private $person; // ... public function setPerson(Person $person) { if ($this->person !== $person) { $this->person = $person; $person->addPhone($this); } } } ``` ``` sql ## create-schema --dump-sql Without the @JoinColumn: CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL); CREATE TABLE phones (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, person_id INTEGER DEFAULT NULL, number VARCHAR(255) NOT NULL) With @JoinColumn(name="person_id", referencedColumnName="id", nullable=false) : CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(255) NOT NULL); CREATE TABLE phones (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, person_id INTEGER NOT NULL, number VARCHAR(255) NOT NULL) ```
admin added the Bug label 2026-01-22 12:55:33 +01:00
admin closed this issue 2026-01-22 12:55:35 +01:00
Author
Owner

@doctrinebot commented on GitHub (Aug 13, 2010):

Comment created by romanb:

Why is NOT NULL expected? We chose to default to nullable foreign keys so associations are optional by default.

So this is not a bug but a request to change the current default.

@doctrinebot commented on GitHub (Aug 13, 2010): Comment created by romanb: Why is NOT NULL expected? We chose to default to nullable foreign keys so associations are optional by default. So this is not a bug but a request to change the current default.
Author
Owner

@doctrinebot commented on GitHub (Aug 13, 2010):

Comment created by @beberlei:

Sorry, i was too tired to realize this is the desired behavior. By default aforeign key is nullable. sorry to bother everyone ;)

@doctrinebot commented on GitHub (Aug 13, 2010): Comment created by @beberlei: Sorry, i was too tired to realize this is the desired behavior. By default aforeign key is nullable. sorry to bother everyone ;)
Author
Owner

@doctrinebot commented on GitHub (Aug 13, 2010):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Aug 13, 2010): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Aug 13, 2010):

Comment created by djones:

I was in a discussion on the #doctrine IRC channel where another user seemed to have DEFAULT NULL without the @JoinColumn explicitly set and I asked @beberlei which was the default behaviour and he said that it should be NULL by default and to open a ticket! If it's not a bug, them I'm sorry for wasting anyone's time.

It might be useful to outline what the defaults are in the Annotation Reference in the manual. I was only asking because I'd checked there first and it wasn't documented as far as I could see.

@doctrinebot commented on GitHub (Aug 13, 2010): Comment created by djones: I was in a discussion on the #doctrine IRC channel where another user seemed to have DEFAULT NULL without the @JoinColumn explicitly set and I asked @beberlei which was the default behaviour and he said that it should be NULL by default and to open a ticket! If it's not a bug, them I'm sorry for wasting anyone's time. It might be useful to outline what the defaults are in the Annotation Reference in the manual. I was only asking because I'd checked there first and it wasn't documented as far as I could see.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#918