Skip to content

Commit

Permalink
fix: QueryBuilder update handles Date objects wrong on a ManyToOne re…
Browse files Browse the repository at this point in the history
…lationship (#8748)

* fixes #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>
  • Loading branch information
3 people committed Aug 25, 2022
1 parent b8d04dc commit 88d0ced
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/query-builder/UpdateQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ export class UpdateQueryBuilder<Entity extends ObjectLiteral>
if (
column.referencedColumn &&
typeof value === "object" &&
!(value instanceof Date) &&
value !== null &&
!Buffer.isBuffer(value)
) {
Expand Down
29 changes: 29 additions & 0 deletions test/github-issues/8747/entity/Car.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
Column,
Entity,
PrimaryGeneratedColumn,
BaseEntity,
OneToMany,
JoinColumn,
ManyToOne,
} from "../../../../src"
import { Record } from "./Record"

@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
}
21 changes: 21 additions & 0 deletions test/github-issues/8747/entity/Record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
Column,
Entity,
BaseEntity,
JoinColumn,
ManyToOne,
} from "../../../../src"
import { Car } from "./Car"

@Entity()
export class Record extends BaseEntity {
@Column({ type: "timestamp", 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
}
52 changes: 52 additions & 0 deletions test/github-issues/8747/issue-8747.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "../../utils/test-setup"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { expect } from "chai"
import { Car } from "./entity/Car"
import { Record } from "./entity/Record"
import { DataSource } from "../../../src"

describe("github issues > #8747 QueryBuilder update handles Date objects wrong on a ManyToOne relationship.", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
enabledDrivers: ["mysql", "postgres", "mariadb"],
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

it("should correctly update the datetime field", async () => {
for (const dataSource of dataSources) {
Car.useDataSource(dataSource)
Record.useDataSource(dataSource)
const car = await Car.create({}).save()

const record = await Record.create({
timestamp: new Date(),
car,
}).save()

await Car.update(
{ uuid: car.uuid },
{ latestRecordTimestamp: record.timestamp },
)

const carReloaded = await Car.findOne({
where: { uuid: car.uuid },
})

expect(carReloaded).to.exist
expect(record.timestamp?.getTime()).to.be.equal(
carReloaded!.latestRecordTimestamp?.getTime(),
)
}
})
})

0 comments on commit 88d0ced

Please sign in to comment.