Skip to content
Advertisement

Nest Postgresql Sequelize one-to-one Error with ForeignKey

My Table

@Table({ tableName: "siCheckingInfo", createdAt: false, updatedAt: false })
export class SiChecking extends Model<SiChecking, CheckingAttrs> {
  @Column({
    type: DataType.INTEGER,
    unique: true,
    autoIncrement: true,
    primaryKey: true,
  })
  pageId: number;

  @Column({ type: DataType.STRING, allowNull: false })
  manufactureNum: string;

  *...some columns...*

  @Column({ type: DataType.STRING, allowNull: true })
  checkingType: string;

  @ForeignKey(() => CheckingDocs)
  @Column({ type: DataType.INTEGER })
  checkingDocsId: number;

  @BelongsTo(() => CheckingDocs)
  doc: CheckingDocs;

  @ForeignKey(() => SiType)
  @Column({ type: DataType.INTEGER })
  siTypeId: number;

  @BelongsTo(() => SiType)
  siType: SiType;

  **@ForeignKey(() => CheckingResults)
  @Column({ type: DataType.INTEGER })
  resultId: number;
  @HasOne(() => CheckingResults)
  results: CheckingResults;**
}

Has one-to-one connection with table

@Table({ tableName: "checkingResults", createdAt: false, updatedAt: false })
export class CheckingResults extends Model<
  CheckingResults,
  CheckingResultsAttrs
> {
  @ForeignKey(() => SiChecking)
  @Column({
    type: DataType.INTEGER,
    unique: true,
    autoIncrement: true,
    primaryKey: true,
  })
  resultId: number;

  @Column({ type: DataType.BOOLEAN, unique: false, allowNull: false })
  isSuccessful: boolean;

*...some columns...*  

  @Column({ type: DataType.BOOLEAN, unique: false, allowNull: true })
  siIsSigned: boolean;

  @BelongsTo(() => SiChecking)
  SiChecking: SiChecking;
}

But when i try add some data in second table< i got an Error – Nest error insert or update on table “checkingResults” violates foreign key constraint “checkingResults_resultId_fkey”

What happened? I try any thing… And how i remember – a don’t get this error before in this place, i don’t touch something in here, but error is here now…

Advertisement

Answer

I just needed to change places in tables with @HasOne and @BelongsTo

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement