使用Sequelize在数据库中创建一对多关系对象。

huangapple go评论71阅读模式
英文:

Create one to many relationship object in database using Sequelize

问题

我搜索了很多,没有找到任何答案。
我的英语不是很好,所以我可能没有使用最好的关键词...

这是我的问题,是否有一种方法可以在一个查询中插入像这样的对象?

我有两个对象之间的一对多关系,定义如下:

foo可以有多个bar

bar可以有一个foo

export const foo = sequelize.define('foos', {
    idfoo: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    value: Sequelize.STRING,
    valueInt: Sequelize.INTEGER
})

export const bar = sequelize.define('bars', {
    idbar: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    value: Sequelize.STRING,
    valueInt: Sequelize.INTEGER
})

foo.hasMany(bar, {
    foreignKey: 'idfoo'
});
bar.belongsTo(foo);

想象一下,你有这个对象,你想插入到你的数据库中。你会怎么做?

{
    "value": "test",
    "valueInt": 5,
    "bars": [
        {
            "value": "test",
            "valueInt": 6
        },
        {
            "value": "test",
            "valueInt": 7
        }
    ]
}

我已经阅读了Sequelize的文档,没有找到这个问题的线索(或者可能我错过了)。我无法让这个解决方案工作https://stackoverflow.com/questions/50685266

实际上,这是我的Sequelize创建函数,我以为Sequelize会自动处理所有的导入(可能有点天真)...

static async post(body) {
    await foo.create(body);
}
英文:

I've searched a lot and didn't find any answer.
My english isn't that good so i might not used the best keywords...

Here is my problem, is there a way to insert in one query an object like this ?

I have a one to many relationship between two objects define like this :

foo can have many bar

bar can have one foo

export const foo = sequelize.define('foos', {
    idfoo: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    value: Sequelize.STRING,
    valueInt: Sequelize.INTEGER
})

export const bar = sequelize.define('bars', {
idbar: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    primaryKey: true
},
    value: Sequelize.STRING,
    valueInt: Sequelize.INTEGER
})

foo.hasMany(bar, {
    foreignKey: 'idfoo'
});
bar.belongsTo(foo);

Imagine you got this object you want to insert in your db. How would you do it ?

{
    "value": "test",
    "valueInt": 5,
    "bars": [
        {
            "value": "test",
            "valueInt": 6
        },
        {
            "value": "test",
            "valueInt": 7
        }
    ]
}

I have read Sequelize documentation i didn't find any clue of this issue (or maybe i've missed it ).

I wasn't be able to make this solution works https://stackoverflow.com/questions/50685266

Actually here is my Sequelize create function, i thought Sequelize handled all the imports alone (pretty naive i guess)...

static async post(body) {
    await foo.create(body);
}

答案1

得分: 0

首先,您需要为配对的关联项指定相同的 foreignKey 选项:

foo.hasMany(bar, {
    foreignKey: 'idfoo'
});
bar.belongsTo(foo, {
    foreignKey: 'idfoo'
});

这样 Sequelize 可以确保这两个模型使用相同的字段相互关联。

其次,为了一起插入主记录和子记录,您需要在 create 调用中的 foo 模型中指定 include 选项,包括 bar 模型:

static async post(body) {
    await foo.create(body, {
        include: [bar]
    });
}
英文:

First, you need to indicate the same foreignKey option for both paired associations:

foo.hasMany(bar, {
    foreignKey: 'idfoo'
});
bar.belongsTo(foo, {
    foreignKey: 'idfoo'
});

That way Sequelize can make sure these two models linked by the same fields from both ends.
Second, in order to insert both main and child records together you need to indicate the include option in the create call with bar model:

static async post(body) {
    await foo.create(body, {
     include: [bar]
    });
}

huangapple
  • 本文由 发表于 2023年1月8日 01:35:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75042437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定