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

When cascade option is specified, nested entities can cause normalizeParams to fail with "Cannot convert undefined or null to object" #140

Open
kevin-mitchell opened this issue Apr 5, 2022 · 0 comments

Comments

@kevin-mitchell
Copy link

kevin-mitchell commented Apr 5, 2022

Package versions and database engine type (please complete the following information):

  • Database Engine: postgres
  • TypeORM Version: ^0.2.42
  • Driver Version ^2.3.8

Describe the bug

When a "parent" entity is saved that has a related entity (e.g. @ManyToOne / @OneToMany) where the relationship is defined in TypeORM with cascade: true, the "cascade" happens and the child entity is saved, however any further nested (within the "child" being saved via cascade) entities are "undefined" and so when normalizeParams is called ... Object.keys(undefined) ... happens causing Cannot convert undefined or null to object.

I'm thinking this may have something to do with the @JoinColumn decorator being added. When I remove it, I don't see this issue.

The below example hopefully will add a bit of clarity.

To Reproduce

The "parent" entity:

...
  @OneToMany('ChannelVersion', 'channel', {
    cascade: true,
  })
  versions!: ChannelVersion[];
...

The "child" entity:

...
  @ManyToOne('Channel', 'versions')
  channel!: Channel;

...

  @ManyToOne('User', 'publishedChannelVersions', {
    eager: true,
    nullable: true,
  })
  @JoinColumn({ name: 'publishedById' })
  publishedBy?: User;

  @Column({ nullable: true })
  publishedById?: string;

...

When the error happens

When the channel is saved, with a channelVersion set on it, the below query is generated and the error is thrown. **Note that the publishedBy and publishedById are both marked as nullable - they should simply not be saved in the database, but instead it seems like the driver is attempting to save the relationship anyway.

Here is the query that is generated (obviously this is not a 100% full example, the important bit is the undefined!)

{
    sql: 'UPDATE "channel_version" SET "updatedById" = :param_1, "publishedById" = :param_2, "publishMetadata" = :param_3, "updatedAt" = :param_4 WHERE "id" IN (:param_5) RETURNING "updatedAt"',
    parameters: [{
    name: 'param_1',
    value: '15132...71a77',
    cast: 'UUID'
  },
  undefined,
  { name: 'param_3', value: '{"slug":"/index"}', cast: 'JSON' },
  {
    name: 'param_4',
    value: '2022-04-05 18:56:01.782',
    cast: 'TIMESTAMP'
  },
  {
    name: 'param_5',
    value: 'e108...9161',
    cast: 'uuid'
  } ],
    transactionId: 'AWfzD...UH2g'
  }

If I remove cascade: true, then perhaps obviously the error does not happen, because when channel is saved, channelVersion is not automatically saved.

Note that if I use the postgres driver (NOT using this data-api driver) there are no errors / everything works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant