// table main_lookup
const main_lookup=DB.connection.define('main_lookup',{
main_lookup_name: {
type :Sequelize.STRING,
primaryKey: true,
allowNull: false
},
value:{
type:Sequelize.JSON,
allowNull:false,
}
});
//表2
const school_lookup= DB.connection.define('school_lookup',{
school_id : {
type :Sequelize.STRING,
allowNull: false,
references: {
model: schools,
key: 'school_id'
}
},
lookup_name: {
type :Sequelize.STRING,
unique: true,
allowNull: false
},
value: {
type: Sequelize.JSON,
allowNull: false,
}
});
我需要将数据从main_lookup表发送到学校查找数据
school_id // that is given by me
lookup_name // that is copy from main_lookup
value // that is copy from main_look_up
示例 主查找// table1
main_lookup_name value
---------------- -----
language ['english','tamil']
subject ['social','maths']
该解决方案类似于此
之后的内容school_lookup // table2 //needed
school_id lookup value
--------- ------ -----
cit language ['english','tamil']
cit subject ['social','maths']
我需要通过简单的方式提供帮助
答案 0 :(得分:0)
您可以使用原始查询执行相同的操作。
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password');
sequelize.query("insert into <table1> select * from <table2>", { type:Sequelize.QueryTypes.SELECT})
.then(function(results) {
console.log(results) // or do whatever you want
})
答案 1 :(得分:-1)
我认为你正在使用sequelize,就像人们如何使用microsoft excel或microsoft访问一样。你可以简单地在续集中建立关系。 这是代码
//校模型
const School=Sequelize.define('school',{
name: {
type :Sequelize.STRING,
allowNull: false
}
});
//受试者
const Subject= Sequelize.define('subject',{
type: {
type :Sequelize.STRING,
unique: true,
allowNull: false
},
value: {
type: Sequelize.ARRAY,
allowNull: false,
}
});
//数据库
const School = require('./path_to_schoolmodel');
const Subject = require('./path_to_subjectmodel');
Subject.belongsTo(School);
School.hasMany(Subject);
学校模式
id name
-- ---------------------
1 'fullstack academy'
2 'app academy'
Subject model
id type value schoolID
-- --------- -------------------- ------------------
1 language ['english','tamil'] 2
2 subject ['social','maths'] 1
如果您设置模型关系,您应该能够看到这样的数据。
然后在服务器端使用Sequelize模型查询时。
const School = require('./path_to_schoolmodel');
const Subject = require('./path_to_subjectmodel');
School.findAll({
include:[ Subject ]
})
.then(console.log)
.catch(console.error)
你的console.log应该返回类似
的内容 [
{
id: 1,
name: 'fullstack academy'
subjects: [{
id: 2,
type: 'subject',
value: [
'socials', 'maths'
]
}]
},
{
id: 2,
name: 'app academy'
subjects: [{
id: 1,
type: 'language',
value: [
'english', 'tamil'
]
}]
},
]