我是新成员,一直在探索协会。我正在使用mysql 5.6和sequelize ^ 4.42.0。我正在尝试创建两个简单的表:PRJS和任务,并将一些数据插入这些表中。
通过以下代码段,我试图将数据一次性插入到“ PRJS”和“ Tasks”表中。
尽管两个表都已创建,但它只是将数据插入到PRJS表中。
const http = require("http");
const express = require("express");
const Sequelize = require("sequelize");
const router = require("./routes/api-routes");
const sequelize = require("./config/db");
const Prj = sequelize.define('prj', {
id :{
type:Sequelize.INTEGER,
autoIncrement:true,
primaryKey:true
},
title: Sequelize.STRING,
description: Sequelize.TEXT
});
const Task = sequelize.define('task', {
details: Sequelize.STRING,
});
Prj.hasMany(Task);
Task.belongsTo(Prj);
Prj.create(
{
title:"a",
description:"asdfasfasd",
tasks:[{
details:"asfasdfasd"
}],
include:[Task]
}
);
以下是我在上面的代码中导入的db.js文件的内容:
const Sequelize = require("sequelize");
const sequelize = new Sequelize("test_db","test","123456",{
dialect:"mysql",
host:"localhost",
port:3306
});
module.exports = sequelize;
非常感谢您的帮助。
谢谢, 拉希里
答案 0 :(得分:0)
Prj.hasMany(Task, { as: 'Tasks' });
Task.belongsTo(Prj);
Prj.create(
{
title:"a",
description:"asdfasfasd",
tasks:[{
details:"asfasdfasd"
}],
{
include: [{
model: Task,
as: 'Tasks'
}]
}
}
);
请尝试并写下结果。