mysql - 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
,但是数据仍然没有被填充到数据库中。
解决方案
heroku 命令格式错误。应该是:heroku run node ./seeds/index.js
。
推荐阅读
- jquery - Laravel vuejs 更改的选定索引
- javascript - 如何将 moment.updateLocale() 迁移到 date-fns 语言环境方法?
- annotations - Jape规则在一组句子中注释问题
- python - Opencv 从我的 python 代码中给出 C++ 异常错误
- android - 无法在 android studio 中设置 firebase
- javascript - 当我从父组件更改其状态时,为什么我的子组件没有更新?
- apache - 具有相同参数的多个页面的htaccess重写规则
- ffmpeg - .avi 文件到带有 alpha 通道的 .webm 文件
- python - 在 Windows 中安装 apex
- python-3.x - 如何将一维数据列表标准化为特定范围?