question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can bidirectional one-to-one relationship have null on non-foreign-key-owning side?

See original GitHub issue

Issue type:

[x] question

Database system/driver:

[x] postgres

I have a one to one relationship like so:

@Entity()
export class Car extends BaseEntity {
	@PrimaryColumn()
	id: number

	@OneToOne(type => User, user => user.car, { nullable: true })
	user?: User
}
@Entity()
export class User extends BaseEntity {
	@PrimaryGeneratedColumn()
	id: number

	@OneToOne(type => Car, car => car.user)
	@JoinColumn()
	car: Car
}

Is it okay for me to able to create Users without creating Cars, and Cars without Users? By making car.user nullable, I’m wanting to make it such that a car can exist without a user, but a user cannot exist without a car.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mantagencommented, Nov 13, 2020

@saoudrizwan

You’ve probably moved on from that particular problem, but if you haven’t or if anyone stumbles on this:

The OneToOne column will be nullable by default. No make it not nullable, see this comment: https://github.com/typeorm/typeorm/issues/4301#issuecomment-634032714

For you it would look something like (EDIT: this code snippet contains an error, see https://github.com/typeorm/typeorm/issues/4553#issuecomment-726717067 for correction)

@Entity()
export class Car extends BaseEntity {
	@PrimaryColumn()
	id: number

	@OneToOne(type => User, user => user.car)
	user: User | null
}
@Entity()
export class User extends BaseEntity {
	@PrimaryGeneratedColumn()
	id: number

        @Column({ type: "integer" })
        userId: number;
        
	@OneToOne(type => Car, car => car.user)
	@JoinColumn()
	car: Car
}

By adding userId, which defaults to nullable: false, a NOT NULL constraint will be added to the userId field in the Car table (which will not be added in your example as you’ve written it)

0reactions
mantagencommented, Dec 29, 2020

@msiric if you want this behaviour then I recommend that you follow the docs here: https://github.com/typeorm/typeorm/blob/master/docs/one-to-one-relations.md

That should give you the behaviour that you want to achieve.

This thread is specifically for adding a “NOT_NULL” constraint to user.carId, which I don’t think is compatible with the way you’re wanting to insert data. I could be wrong.

For other questions, please check out the community slack or check TypeORM’s documentation page on other support avenues - cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bidirectional association with one to many side as owner and ...
I understand that in the world of databases the owner of the above relationship would be soldier since it has the column with...
Read more >
The best way to map a @OneToOne relationship with JPA and ...
Learn the best way to map a OneToOne association with JPA and Hibernate when using both unidirectional and bidirectional relationships.
Read more >
JPA / Hibernate - Mapping One-to-One relationships - Nullbeans
We discussed why we would need to map the relationship in a bidirectional manner and how to do it in a way to...
Read more >
Introduction to Spring Data JPA - Part 6 Bidirectional One to ...
The next article in this series introduces you to bidirectional one-to-one relations in Spring Data.
Read more >
OneToOne bidirectional StackOverflow error - Google Groups
As you can see I need this bidirectional OneToOne relationship in order to get Booking entity when having Invoice entity. Without JoinColumn on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found