Can bidirectional one-to-one relationship have null on non-foreign-key-owning side?
See original GitHub issueIssue 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 User
s without creating Car
s, and Car
s without User
s?
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:
- Created 4 years ago
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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)
By adding
userId
, which defaults tonullable: false
, aNOT NULL
constraint will be added to theuserId
field in the Car table (which will not be added in your example as you’ve written it)@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.