首页 > 解决方案 > Heroku 上的种子 MySQL 数据库

问题描述

我有一个位于 ./seeds/index.js 的种子文件,当在本地调用它时,它将创建表并为 MySQL 数据库播种数据。

const sequelize = require("../config/connection");
const User = require("../models/User");
const Post = require("../models/Post");
const Comment = require("../models/Comment");
const userData = require("./user-seeds.json");
const postData = require("./post-seeds.json");
const commentData = require("./comment-seeds.json");

//create tables and seed with test data
const seedDatabase = async () => {
    await sequelize.sync({ force: true });

    await User.bulkCreate(userData, {
        individualHooks: true,
        returning: true,
    });

    await Post.bulkCreate(postData, {
        individualHooks: true,
        returning: true,
    });

    await Comment.bulkCreate(commentData, {
        individualHooks: true,
        returning: true,
    });

    process.exit(0);
};

seedDatabase();

一旦应用程序部署在heroku上,我希望它也能运行。

我试过跑步heroku run ./seeds/index.js但得到Permission denied在此处输入图像描述

我还创建了一个 Procfile 并添加了以下行:worker: node ./seeds/index.js它看起来像在部署时运行: 在此处输入图像描述

但是数据库仍然是空的并且没有种子。

如何在部署到 Heroku 期间或之后使用 node.js 文件为 MySQL 数据库播种?

编辑:我应该补充一点,我知道数据库已连接到应用程序,因为如果表在应用程序启动时不存在,则会创建表。然而,播种仍然没有发生。

编辑 2:我的 Procfile 的内容格式错误。我相信它应该是worker: node ./seeds/index.js,但是数据仍然没有被填充到数据库中。

标签: mysqlnode.jsherokusequelize.js

解决方案


heroku 命令格式错误。应该是:heroku run node ./seeds/index.js


推荐阅读