mysql - mysql 种子中的异步等待不运行

标签 mysql reactjs async-await sequelize.js

我正在尝试为我的 MySQL 数据库做种。我正在使用 Sequelize ORM。在我的模型文件夹中的 index.js 文件中,我有代码来为每个模型运行 realSync() 函数,如下所示:

const syncDB = async () => {
await db['Meal'].realSync();
await db['User'].realSync();
}
syncDB();

在我的“膳食”文件中,我有以下内容:
const mealSeeds = require("../scripts/mealSeeds");
module.exports = (sequelize, DataTypes) => {
let Meal = sequelize.define("Meal", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: DataTypes.STRING,
type: DataTypes.STRING,
description: DataTypes.STRING,
photo_URL: DataTypes.STRING,
allergen_dairy: DataTypes.BOOLEAN,
allergen_treenuts: DataTypes.BOOLEAN,
allergen_peanuts: DataTypes.BOOLEAN,
allergen_wheat: DataTypes.BOOLEAN,
allergen_fish: DataTypes.BOOLEAN,
allergen_crustaceanshellfish: DataTypes.BOOLEAN,
allergen_eggs: DataTypes.BOOLEAN,
allergen_soya: DataTypes.BOOLEAN,
date_available: DataTypes.DATE,
time_available: DataTypes.TIME,
quantity: DataTypes.INTEGER,
zipcodes: DataTypes.JSON,
catererId: {
field: "CatererId",
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0
}
})
Meal.associate = function (models) {
Meal.belongsTo(models.User, {
foreignKey: "catererId",
targetKey: "id"
})

}
// // Insert the meal seed data
Meal.realSync = async () => {
await Meal.sync()
return await Meal.bulkCreate(mealSeeds,
{ignoreDuplicates: true}
);
};
return Meal;
}

Meal.realSync 应该用脚本目录中的 mealSeeds.js 文件中的数据为 Meals 表做种子。 (我有一个 User.js 文件,其中包含用户表字段和类似的用户表的 .realSync() 函数。这个函数工作得很好,用户被植入数据库中)。

这个功能可以正常工作数周,因为我正在构建项目,最近在更改“膳食”表中的某些字段后,它不再起作用。我之前的研究表明,通过在 index.js 文件中异步调用 realSync() 函数,它会运行并等待 Meal realSync() 函数完成,然后再运行 User realSync() 函数。我不确定为什么它根本不再运行第一个函数。任何帮助将不胜感激。

最佳答案

解决了-我发现我的种子数据不包含外键引用。

关于mysql - mysql 种子中的异步等待不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117516/

相关文章:

MySQL:动态导出带标题的 CSV 文件

java - 在 Spring 中创建自定义查询

mysql - 查找带有 tag1 和 tag2 的帖子? (使用连接表)存在/具有/子查询...使用什么?

c# - 如何异步初始化静态类

php - 更新 MySQLI 中的参数

reactjs - .NET Core React SPA 中的环境变量

javascript - 升级到 React Native 0.63.0 后 Pod 安装错误

javascript - React.js 组件中的 Medium.js 使用 invokeElement

c# - 在异步等待中等待的工作

c# - 当我的应用程序启动时,如何在我的 Xamarin Forms 中调用此异步方法?