【问题标题】:Seed MySQL database on HerokuHeroku 上的种子 MySQL 数据库
【发布时间】:2021-11-10 17:42:35
【问题描述】:

我有一个位于 ./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,但是数据库中仍然没有填充数据。

【问题讨论】:

    标签: mysql node.js heroku sequelize.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多