首页 > 解决方案 > [SequelizeDatabaseError]:关系 « » 不存在

问题描述

我想在我的数据库中使用 typescript sql 和 sequelize 创建一些表问题是:当我尝试将我的表同步到我的数据库时,它会无序地创建表这是一个例子:我想在纠正表之前创建 Equipement引用现有表,但 sequelize 在 Equipement 之前创建更正表

我尝试使用异步/等待方法,但我不知道如何调用异步方法来执行它?

如果有另一种方法可以创建具有特定顺序的表,请告诉我

第一个模型



import {database} from "../config/database"
import { Model,DataTypes } from "sequelize"

export interface EquipementInterface{
    equipment_machine: string;
    atelier: string;
    etat_station: string;
    R_restriction: string;
    pieces_remplaces: string;
    operations_effectuees: string;
}

export class Equipement extends Model{
    id_equipement!: number;
    equipment_machine!: string;
    atelier!: string;    
    etat_station!: string;
    R_restriction!: string;
    pieces_remplaces!: string;
    operations_effectuees!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;
}

Equipement.init(
    {
        id_equipement: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        
        equipment_machine: {
            type: new DataTypes.STRING(50),
            allowNull: false,
        },
        atelier: {
            type: new DataTypes.STRING(20),
            allowNull: false,
        },
        etat_station: {
            type: new DataTypes.STRING(20),
            allowNull: false,
        },
        R_restriction: {
            type: new DataTypes.STRING(255),
            allowNull: false,
        },
        pieces_remplaces:{
            type: new DataTypes.STRING(100),
            allowNull: false,
        },
        operations_effectuees:{
            type: new DataTypes.TEXT,
            allowNull: false,
        }
    },
    {
        tableName: "equipement",
        sequelize: database,
    }
);

 Equipement.sync()
        .then(() => console.log("Equipement table synchronized"))
        .catch(err => console.log("Equipement Sync Error: ", err));

参考它的第二个模型


import { database } from "../config/database"
import { Model, DataTypes } from "sequelize"

export interface CorrectiveInterface {
    anomalies_constatees: string;
    ref_manip: string;
    nom_technicien: string;
}

export class Corrective extends Model {
    id_corrective!: number;
    fk_equipement_id!: number;
    anomalies_constatees!: string;
    ref_manip!: string;
    nom_technicien!: string;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

}

Corrective.init(
    {
        id_corrective: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        fk_equipement_id: {
            type: new DataTypes.INTEGER,
            allowNull: false,
            references: { model: "Equipement", key: "id_equipement" },
        },

        anomalies_constatees: {
            type: new DataTypes.TEXT,
            allowNull: false,
        },
        ref_manip: {
            type: new DataTypes.STRING(50),
            allowNull: false,
        },
        nom_technicien: {
            type: new DataTypes.STRING(50),
            allowNull: false,
        }
    },
    {
        tableName: "corrective",
        sequelize: database,
    }
);

Corrective.sync()
    .then(() => console.log("Corrective table synchronized"))
    .catch(err => console.log("Corrective Sync Error: ", err));

标签: sequelize.jssequelize-typescript

解决方案


推荐阅读