Skip to content

Three foreign keys referencing same table #266

@devuxer

Description

@devuxer

My Transit model has three foreign keys referencing the Nation model.

When I sync to a SQLite database, however, only the first foreign key gets created.

Am I doing something wrong?

Model

import { AllowNull, BelongsTo, Column, DataType, ForeignKey, Model, PrimaryKey, Table } from "sequelize-typescript";
import Nation from "./Nation";
import Plan from "./Plan";

@Table({ tableName: "Transits" })
export default class Transit extends Model<Transit> {
    @AllowNull(false)
    @PrimaryKey
    @Column(DataType.TEXT)
    public id: string;

    @AllowNull(false)
    @ForeignKey(() => Plan)
    @Column(DataType.TEXT)
    public planId: string;

    @BelongsTo(() => Plan, { onDelete: "cascade" })
    public plan: Plan;

    @AllowNull(false)
    @ForeignKey(() => Nation)
    @Column(DataType.TEXT)
    public originNationId: string;

    @BelongsTo(() => Nation, { onDelete: "no action" })
    public originNation: Nation;

    @AllowNull(false)
    @ForeignKey(() => Nation)
    @Column(DataType.TEXT)
    public destinationNationId: string;

    @BelongsTo(() => Nation, { onDelete: "no action" })
    public destinationNation: Nation;

    @AllowNull(false)
    @Column(DataType.TEXT)
    public vesselName: string;

    @AllowNull(false)
    @ForeignKey(() => Nation)
    @Column(DataType.TEXT)
    public vesselFlagNationId: string;

    @BelongsTo(() => Nation, { onDelete: "no action" })
    public vesselFlagNation: Nation;

    @AllowNull(false)
    @Column(DataType.TEXT)
    public beginDate: string;

    @AllowNull(false)
    @Column(DataType.TEXT)
    public endDate: string;

    @AllowNull(true)
    @Column(DataType.TEXT)
    public details: string;

    @AllowNull(false)
    @Column(DataType.INTEGER)
    public isFirm: boolean;
}

Expected Result (3 references to "Nations")

CREATE TABLE `Transits` (
	`id`	TEXT NOT NULL,
	`planId`	TEXT NOT NULL,
	`originNationId`	TEXT NOT NULL,
	`destinationNationId`	TEXT NOT NULL,
	`vesselName`	TEXT NOT NULL,
	`vesselFlagNationId`	TEXT NOT NULL,
	`beginDate`	TEXT NOT NULL,
	`endDate`	TEXT NOT NULL,
	`details`	TEXT,
	`isFirm`	INTEGER NOT NULL,
	FOREIGN KEY(`destinationNationId`) REFERENCES `Nations`(`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
	FOREIGN KEY(`vesselFlagNationId`) REFERENCES `Nations`(`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
	PRIMARY KEY(`id`),
	FOREIGN KEY(`planId`) REFERENCES `Plans`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
	FOREIGN KEY(`originNationId`) REFERENCES `Nations`(`id`) ON DELETE NO ACTION ON UPDATE CASCADE
);

Actual Result (1 reference to "Nations")

CREATE TABLE `Transits` (
	`id`	TEXT NOT NULL,
	`planId`	TEXT NOT NULL,
	`originNationId`	TEXT NOT NULL,
	`destinationNationId`	TEXT NOT NULL,
	`vesselName`	TEXT NOT NULL,
	`vesselFlagNationId`	TEXT NOT NULL,
	`beginDate`	TEXT NOT NULL,
	`endDate`	TEXT NOT NULL,
	`details`	TEXT,
	`isFirm`	INTEGER NOT NULL,
	FOREIGN KEY(`planId`) REFERENCES `Plans`(`id`) ON DELETE CASCADE ON UPDATE CASCADE,
	PRIMARY KEY(`id`),
	FOREIGN KEY(`originNationId`) REFERENCES `Nations`(`id`) ON DELETE NO ACTION ON UPDATE CASCADE
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions