sequelize.js - [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));
解决方案
推荐阅读
- flutter - Flutter - 如何格式化电话号码文本字段
- android - Subject 忽略了另一个 observable 的 onNext
- powershell - Powershell 包含 - 匹配部分字符串和列表?
- angular - 如何在表格中制作组标题?
- jquery - 表单验证后如何使用ajax发布表单?
- continuous-integration - 特定分支名称中的 GITLAB-CI
- node.js - laravel npm run production 编译成功,但是 npm ERR!代码生命周期
- ssl - 基于 SSL 的 EJB 远程处理
- discord - 为什么我的 args[1] 在这种情况下不起作用
- python - 将特征添加到时间序列预测模型输入的适当方法