Skip to content

Create instance with many-to-many association with extra column #10034

Closed

Description

@papb

What are you doing?

I'm trying to create an instance along with its many-to-many association which has an extra column in the junction table:

const Sequelize = require('sequelize');
const sequelize = new Sequelize({ dialect: 'sqlite', storage: 'db.sqlite' });

const Foo = sequelize.define("foo", { name: Sequelize.STRING });
const Bar = sequelize.define("bar", { name: Sequelize.STRING });
const Foo_Bar = sequelize.define("foo_bar", { baz: Sequelize.STRING });
Foo.belongsToMany(Bar, { through: Foo_Bar });
Bar.belongsToMany(Foo, { through: Foo_Bar });

sequelize.sync().then(() => {
    return Foo.create({
        name: "foo...",
        bars: [
            {
                name: "bar...",
                foo_bar: {
                    baz: "baz..."
                }
            }
        ]
    }, { include: Bar });
}).then(() => {
    return Foo.findOne({ include: Bar });
}).then(result => {
    console.log(JSON.stringify(result, null, 4));
});

What do you expect to happen?

I expected the following output (showing that everything was created successfully):

{
    "id": 1,
    "name": "foo...",
    "bars": [
        {
            "id": 1,
            "name": "bar...",
            "foo_bar": {
                "baz": "baz...",
                "fooId": 1,
                "barId": 1
            }
        }
    ]
}

What is actually happening?

TypeError: Cannot read property 'indexOf' of undefined
    at model.set (temp/node_modules/sequelize/lib/model.js:3379:22)
    at Promise.map.instance (temp/node_modules/sequelize/lib/model.js:3759:26)

Dialect: Tested with sqlite but could affect others too
Dialect version: sqlite3 3.1.13 and 4.0.2
Sequelize version: 4.39.0 and 5.0.0-beta.12
Tested with latest release: Yes (5.0.0-beta.12)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions