How to add data to the sequelize model under the action of four types of associations [unfinished]

Suppose there are two models, a stu (student) model and a score (score) model. So how to add the score of the new student while adding the new student.

1. Set stu and score to score: stu = 1: 1

方案1:有别称
models.Score.belongsTo(models.Stu, {
    as: 'user', 
    foreignKey: 'userId',
    targetKey: 'id'
});
models.Score.create({
    user: {
        name: '添加用户',
        age: 50,
        sex: '女',
    },
    projectName: '历史',
    projectScore: '150'
}, {
    raw: true,
    include: [{
        model: models.Stu,
        as: 'user'
    }]
}).then((res) =>{
    console.log(JSON.stringify(res, null, 2))
}).catch((err) => {
    console.log(err)
});


方案2:无别称
models.Score.belongsTo(models.Stu, {
    foreignKey: 'userId',
    targetKey: 'id'
});
models.Score.create({
    stu: {
        name: '添加用户',
        age: 50,
        sex: '女',
    },
    projectName: '历史',
    projectScore: '150'
}, {
    raw: true,
    include: [{
        model: models.Stu
    }]
}).then((res) =>{
    console.log(JSON.stringify(res, null, 2))
}).catch((err) => {
    console.log(err)
});

In the 1:1 relationship type, normal addition operations can be performed with or without aliases 

 2. Set stu and score to stu:score=1:n

      2.1. Without another name

models.Stu.hasMany(models.Score, {
    foreignKey: 'userId', // 对于一对多关系,外键在于多的那一方
    targetKey: 'id'
})

models.Stu.create({
    name: '添加用户',
    age: 50,
    sex: '女',
    score: {
        projectName: '历史',
        projectScore: '150'
    }
}, {
    raw: true,
    include: [{
        model: models.Score
    }]
}).then((res) =>{
    console.log(JSON.stringify(res, null, 2))
}).catch((err) => {
    console.log(err)
});

   When no alias is set for score, the normal default should be the table name score, but when it is executed, it is found that the sql statement printed on the console is as follows: only the information of stu is added, and score will not be added like 1:1.

 

   2.2 In the case of another name

models.Stu.hasMany(models.Score, {
    foreignKey: 'userId', // 对于一对多关系,外键在于多的那一方
    targetKey: 'id',
    as: 'score'
})

models.Stu.create({
    name: '添加用户',
    age: 50,
    sex: '女',
    score: {
        projectName: '历史',
        projectScore: '150'
    }
}, {
    raw: true,
    include: [{
        model: models.Score,
        as: 'score'
    }]
}).then((res) =>{
    console.log(JSON.stringify(res, null, 2))
}).catch((err) => {
    console.log(err)
});

  In the case of another name, execute the above code, and you can see that the message printed on the console has added both stu information and score information.

 

3. To be continued

 

 

In summary:

In the 1:1 relationship type, whether there is an alias has no effect on the adding result, but in the 1:n relationship type, if the alias is not added, the data cannot be added to the database normally. The reason is unknown for the time being, if anyone knows, please point it out! 

   

 

Guess you like

Origin blog.csdn.net/QiZi_Zpl/article/details/104967858