Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save entity with ManyToOne with nullable: false set? #3229

Closed
draoncc opened this issue Dec 11, 2018 · 1 comment
Closed

How to save entity with ManyToOne with nullable: false set? #3229

draoncc opened this issue Dec 11, 2018 · 1 comment
Labels

Comments

@draoncc
Copy link

draoncc commented Dec 11, 2018

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I'm terribly tired so apologies if the answer is obvious, and I'm just blind and not seeing it.

Assuming the following two entities:

@Entity()
export class Parent {
  @PrimaryGeneratedColumn()
  id!: number

  @OneToMany(type => Child, child => child.parent)
  children!: Child[]
}

@Entity()
export class Child {
  @PrimaryGeneratedColumn()
  id!: number

  @ManyToOne(type => Parent, parent => parent.children, { nullable: false })
  parent!: Parent
}

When doing entityManager.insert(Child, {}), naturally the error null value in column \"parentId\" violates not-null constraint is thrown. When trying to do entityManager.insert(Child, { parentId: 1 }), the same error is thrown though.

So how do I create and save new Childs with the above model?

@vlapo
Copy link
Contributor

vlapo commented Dec 12, 2018

Your Child entity do not have defined parentId column in entity class. I am getting error in IDE

Object literal may only specify known properties, and 'parentId' does not exist in type 'QueryPartialEntity<Child> | QueryPartialEntity<Child>[]'. [2345]

So typeorm will ignore this property value during query build. Of course typeorm know about this join column but entity argument of insert is QueryPartialEntity<Child>|(QueryPartialEntity<Child>[]) so does not expect parentId property.

There are two options:

  1. Define additional parentId column in Child class.
@Entity()
export class Child {
    @PrimaryGeneratedColumn()
    id!: number;

    @Column()
    parentId: number;

    @ManyToOne(type => Parent, parent => parent.children, { nullable: false })
    parent!: Parent;
}
  1. Use little trick and call insert method like this:
await entityManager.insert(Child, { parent: { id: 1 } as Parent });

@draoncc draoncc closed this as completed Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants