Skip to content

Commit

Permalink
fix: entity value for date columns that are related (#8027)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansoltman committed Aug 8, 2021
1 parent ba366f2 commit 5a3767f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/metadata/ColumnMetadata.ts
Expand Up @@ -615,7 +615,7 @@ export class ColumnMetadata {
if (relatedEntity && relatedEntity instanceof Object && !(relatedEntity instanceof FindOperator) && !(relatedEntity instanceof Buffer)) {
value = this.referencedColumn.getEntityValue(relatedEntity);

} else if (embeddedObject[this.propertyName] && embeddedObject[this.propertyName] instanceof Object && !(embeddedObject[this.propertyName] instanceof FindOperator) && !(embeddedObject[this.propertyName] instanceof Buffer)) {
} else if (embeddedObject[this.propertyName] && embeddedObject[this.propertyName] instanceof Object && !(embeddedObject[this.propertyName] instanceof FindOperator) && !(embeddedObject[this.propertyName] instanceof Buffer) && !(embeddedObject[this.propertyName] instanceof Date)) {
value = this.referencedColumn.getEntityValue(embeddedObject[this.propertyName]);

} else {
Expand All @@ -639,7 +639,7 @@ export class ColumnMetadata {
if (relatedEntity && relatedEntity instanceof Object && !(relatedEntity instanceof FindOperator) && !(relatedEntity instanceof Function) && !(relatedEntity instanceof Buffer)) {
value = this.referencedColumn.getEntityValue(relatedEntity);

} else if (entity[this.propertyName] && entity[this.propertyName] instanceof Object && !(entity[this.propertyName] instanceof FindOperator) && !(entity[this.propertyName] instanceof Function) && !(entity[this.propertyName] instanceof Buffer)) {
} else if (entity[this.propertyName] && entity[this.propertyName] instanceof Object && !(entity[this.propertyName] instanceof FindOperator) && !(entity[this.propertyName] instanceof Function) && !(entity[this.propertyName] instanceof Buffer) && !(entity[this.propertyName] instanceof Date)) {
value = this.referencedColumn.getEntityValue(entity[this.propertyName]);

} else {
Expand Down
14 changes: 14 additions & 0 deletions test/github-issues/8026/entity/Sailing.ts
@@ -0,0 +1,14 @@
import { BaseEntity, Entity, JoinColumn, OneToMany, PrimaryColumn } from "../../../../src";
import { ScheduledSailing } from "./ScheduledSailing";

@Entity()
export class Sailing extends BaseEntity {
@PrimaryColumn()
scheduled_departure_time: Date;

@OneToMany(() => ScheduledSailing, (scheduledSailing) => scheduledSailing.sailing)
@JoinColumn([
{ referencedColumnName: "scheduled_departure_time", name: "scheduled_departure_time" }
])
scheduled_sailings: ScheduledSailing[];
}
17 changes: 17 additions & 0 deletions test/github-issues/8026/entity/ScheduledSailing.ts
@@ -0,0 +1,17 @@
import { BaseEntity, Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from "../../../../src";
import { Sailing } from "./Sailing";

@Entity()
export class ScheduledSailing extends BaseEntity {
@PrimaryColumn()
scheduled_departure_time: Date;

@Column()
scheduled_arrival_time: Date;

@ManyToOne(() => Sailing, (sailing) => sailing.scheduled_sailings)
@JoinColumn([
{ referencedColumnName: "scheduled_departure_time", name: "scheduled_departure_time" }
])
sailing: Sailing;
}
40 changes: 40 additions & 0 deletions test/github-issues/8026/issue-8026.ts
@@ -0,0 +1,40 @@
import { expect } from "chai";
import "reflect-metadata";
import { Connection } from "../../../src";
import { closeTestingConnections, createTestingConnections } from "../../utils/test-utils";
import { Sailing } from "./entity/Sailing";
import { ScheduledSailing } from "./entity/ScheduledSailing";

describe("github issues > #8026 Inserting a value for a column that has a relation, and is also a date, results in the value being inserted as DEFAULT", () => {

let connections: Connection[];

before(async () => connections = await createTestingConnections({
entities: [Sailing, ScheduledSailing],
schemaCreate: true,
dropSchema: true
}));

after(() => closeTestingConnections(connections));

it("it should include a related date column in the constructed query", async () => await Promise.all(connections.map(async connection => {

let queryBuilder = await connection.createQueryBuilder();

const insertValue = {
scheduled_departure_time: new Date(),
scheduled_arrival_time: new Date(),
}

const [query, params] = await queryBuilder
.insert()
.into(ScheduledSailing)
.values([insertValue])
.getQueryAndParameters()

expect(query.includes("DEFAULT")).to.be.false;
expect(params).length(2);

})));

});

0 comments on commit 5a3767f

Please sign in to comment.