-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: QueryBuilder update handles Date objects wrong on a ManyToOne re…
…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
1 parent
b8d04dc
commit 88d0ced
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
} | ||
}) | ||
}) |