A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/79617832/sequelize-magic-method-not-adding-foreign-key below:

javascript - Sequelize magic method not adding foreign key

Sequelize's createXXXXX method doesn't seem to be adding the necessary foreign key.

This running the following code for these two models gives me a "notNull Violation: Seat.office_id cannot be null" error. The docs seem to indicate that that column should be automatically populated, no?

class Seat extends Model {
    static init(sequelize) {
        super.init(
            {
                id: {
                    autoIncrement: true,
                    type: DataTypes.INTEGER,
                    allowNull: false,
                    primaryKey: true,
                },
                office_id: {
                    type: DataTypes.INTEGER,
                    allowNull: false,
                },
                name: {
                    type: DataTypes.STRING,
                    allowNull: true,
                },
            },
            {
                sequelize,
                tableName: "seat",
                schema: "public",
                timestamps: false,
                indexes: [
                    {
                        name: "seat_pkey",
                        unique: true,
                        fields: [{ name: "id" }],
                    },
                ],
            },
        );
    }

    static associate(models) {
        this.belongsTo(models.Office, {
            foreignKey: "office_id",
            as: "office",
        });
    }
}

class Office extends Model {
    static init(sequelize) {
        super.init(
            {
                id: {
                    autoIncrement: true,
                    type: DataTypes.INTEGER,
                    allowNull: false,
                    primaryKey: true,
                },
                title: {
                    type: DataTypes.STRING,
                    allowNull: true,
                },
                description: {
                    type: DataTypes.STRING,
                    allowNull: true,
                },
            },
            {
                sequelize,
                tableName: "office",
                schema: "public",
                timestamps: false,
                indexes: [
                    {
                        name: "office_pkey",
                        unique: true,
                        fields: [{ name: "id" }],
                    },
                ],
            },
        );
    }

    static associate(models) {
        this.hasMany(models.Seat, {
            foreignKey: "office_id",
            as: "seats",
        });
    }
}

let office = await Office.findByPk(1);
let seat = await Seat.build({
  name: "Test",
});

office.createSeat(seat);

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4