Kononnable/typeorm-model-generator

QueryBuilder Insert Issue with Foreign Keys

kjohnson2021 opened this issue · 2 comments

I have two tables - phases and phaseJobs. The phaseJobs table has a foreign key constraint with phases on the phaseId column. Typeorm-model-generator appears to create the correct ManyToOne relationships in the entity TS files; however, when I try to insert a record into phaseJobs, phaseId isn't a valid column.

If I don't use foreign key, it works fine. Select inner join statements also work fine.

Do I need to reference the Phases table in the insert statement?

insertIntoPhaseJobs:

import "reflect-metadata";
import {createConnection} from "typeorm";
import {getConnection} from "typeorm";
import {PhaseJobs} from "./entity/PhaseJobs"

createConnection().then(async connection => {

    //get total estimated hours by month
    await getConnection()
        .createQueryBuilder()
        .insert()
        .into(PhaseJobs)
        .values([
            { organizationId: 2, jobCapacity:.3, jobPtg:.3, phaseId:5 }
            ])
        .execute();
    //  .getSql();

}).catch(error => console.log(error));

Entity ts files for Phases.ts and PhaseJobs.ts:

Phases.ts:

import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from "typeorm";
import { PhaseJobs } from "./PhaseJobs";
import { RoadmapTemplates } from "./RoadmapTemplates";
import { Organizations } from "./Organizations";

@Index("phases_pk", ["phaseId"], { unique: true })
@entity("phases", { schema: "dbo" })
export class Phases {
@PrimaryGeneratedColumn({ type: "int", name: "phaseId" })
phaseId: number;

@Column("nvarchar", { name: "phase", length: 50 })
phase: string;

@Column("decimal", {
  name: "phasePtg",
  nullable: true,
  precision: 6,
  scale: 2,
})
phasePtg: number | null;

@OneToMany(() => PhaseJobs, (phaseJobs) => phaseJobs.phase)
phaseJobs: PhaseJobs[];

@ManyToOne(
  () => RoadmapTemplates,
  (roadmapTemplates) => roadmapTemplates.phases
)
@JoinColumn([{ name: "templateId", referencedColumnName: "templateId" }])
template: RoadmapTemplates;

@ManyToOne(() => Organizations, (organizations) => organizations.phases)
@JoinColumn([
  { name: "organizationId", referencedColumnName: "organizationId" },
])
organization: Organizations;

}

PhaseJobs.ts:

import {
Column,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from "typeorm";
import { JobMaster } from "./JobMaster";
import { Phases } from "./Phases";

@Index("phaseJobs_pk", ["pjId"], { unique: true })
@entity("phaseJobs", { schema: "dbo" })
export class PhaseJobs {
@column("decimal", { name: "jobPtg", nullable: true, precision: 6, scale: 2 })
jobPtg: number | null;

@Column("int", { name: "organizationId" })
organizationId: number;

@Column("decimal", {
  name: "jobCapacity",
  nullable: true,
  precision: 6,
  scale: 2,
})
jobCapacity: number | null;

@PrimaryGeneratedColumn({ type: "int", name: "pjId" })
pjId: number;

@ManyToOne(() => JobMaster, (jobMaster) => jobMaster.phaseJobs)
@JoinColumn([{ name: "jobMasterId", referencedColumnName: "jobMasterId" }])
jobMaster: JobMaster;

@ManyToOne(() => Phases, (phases) => phases.phaseJobs)
@JoinColumn([{ name: "phaseId", referencedColumnName: "phaseId" }])
phase: Phases;

}

Never mind - I figured it out. Even though the ManyToOne relationships are correctly defined in the entity files, you still have to add a @column reference.

@column("int", { name: "phaseId" })
phaseId: number;

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.