Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key by default is null #607

Open
mdrokz opened this issue Oct 15, 2023 · 0 comments
Open

Foreign key by default is null #607

mdrokz opened this issue Oct 15, 2023 · 0 comments

Comments

@mdrokz
Copy link

mdrokz commented Oct 15, 2023

Issue Description

I was going through the association docs to create foreign key constraints for my tables in sqlite but i noticed when using Model.HasMany(Child) by default the foreign key definition is allowNull: true which defeats the purpose of using a foreign key constraint.

https://sequelize.org/docs/v6/core-concepts/assocs/

Im not sure if i missed anything but i couldnt find this anywhere in the docs and the associations docs doesnt explain this behavior properly.

Let me know if i'm missing anything

Additional context

This code would define the foreign key employeeId with allowNull:true

sequelize.define("Employees", {
    id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    employeeType: {
        type: DataTypes.INTEGER,
        allowNull: false
    }

}, {
    indexes: [
        {
            using: "BTREE",
            fields: ["employeeType"]
        }
    ]
})


sequelize.define("Missions", {
    id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
    hours: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    missionName: {
        type: DataTypes.STRING,
        allowNull: true
    },
    missionDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    missionType: {
        type: DataTypes.INTEGER,
        allowNull: true
    }
}
);


// 1 to many relationship between employees & missions
sequelize.models.Employees.hasMany(sequelize.models.Missions);

I had to refactor the code to this for the foreign key constraint to be effective

sequelize.define("Employees", {
    id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    employeeType: {
        type: DataTypes.INTEGER,
        allowNull: false
    }

}, {
    indexes: [
        {
            using: "BTREE",
            fields: ["employeeType"]
        }
    ]
})


sequelize.define("Missions", {
    id: {
        type: DataTypes.UUIDV4,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
    },
    employeeId: {
        type: DataTypes.UUIDV4,
        allowNull: false,
    },
    hours: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    missionName: {
        type: DataTypes.STRING,
        allowNull: true
    },
    missionDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    missionType: {
        type: DataTypes.INTEGER,
        allowNull: true
    }
}
);


// 1 to many relationship between employees & missions
sequelize.models.Employees.hasMany(sequelize.models.Missions, {
    foreignKey: 'employeeId',
    as: 'missions'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant