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

QueryBuilder update handles Date objects wrong on a ManyToOne relation ship. #8747

Closed
jorenvandeweyer opened this issue Mar 14, 2022 · 0 comments · Fixed by #8748
Closed

Comments

@jorenvandeweyer
Copy link
Collaborator

jorenvandeweyer commented Mar 14, 2022

Issue Description

When updating a foreign key that uses a Date object, TypeORM seems to ignore the Date object.

Expected Behavior

Correctly handling the Date object

Actual Behavior

Some internal code is handling the Date object wrong.

Steps to Reproduce

@Entity()
export class Car extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
    uuid: string

  @Column({ type: 'timestamp', precision: 3, nullable: true })
    latestRecordTimestamp?: Date

  @OneToMany(() => Record, record => record.car)
    records: Record[]

  @ManyToOne(() => Record)
  @JoinColumn([
    { name: 'uuid', referencedColumnName: 'carUuid' },
    { name: 'latestRecordTimestamp', referencedColumnName: 'timestamp' }
  ])
    latestRecord?: Record
}
@Entity()
export class Record extends BaseEntity {
  @CreateDateColumn({ precision: 3, primary: true })
    timestamp: Date

  @Column({ type: 'uuid', primary: true })
    carUuid: string

  @ManyToOne(() => Car, car => car.records, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'carUuid' })
    car: Car
}
await getManager().createQueryBuilder()
  .update(Car)
  .set({ latestRecordTimestamp: record.timestamp })
  .where('uuid = :uuid', { uuid: car.uuid })
  .execute()

Temporary Workaround

await getManager().createQueryBuilder()
  .update(Car)
  .set({ latestRecordTimestamp: record.timestamp.toISOString() })
  .where('uuid = :uuid', { uuid: car.uuid })
  .execute()

My Environment

Dependency Version
Operating System MacOS 12.1
Node.js version v16.13.0
Typescript version 4.5.5
TypeORM version 0.2.45

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
jorenvandeweyer added a commit to jorenvandeweyer/typeorm that referenced this issue Mar 14, 2022
jorenvandeweyer added a commit to jorenvandeweyer/typeorm that referenced this issue Mar 14, 2022
@jorenvandeweyer jorenvandeweyer mentioned this issue Mar 14, 2022
7 tasks
nordinh pushed a commit to nordinh/typeorm that referenced this issue Aug 29, 2022
…lationship (typeorm#8748)

* fixes typeorm#8747

* apply fix again

* added test for issue 8747

* format

* remove .only

* only use postgres driver

* fixed test

* removed createDateColumn

* sqlite does not support timestamp type

* fix typo

Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
Co-authored-by: Alex Messer <dmzt08@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant