Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Foreign key by default is null

Open
#607 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
45/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
sqlite, typescript
Domain
documentation

Research direction

Start with the associations documentation at the linked URL and verify how hasMany defines the foreign key and its allowNull behavior. Done means the page clearly documents the default behavior and the explicit configuration needed when a non-null foreign key constraint is intended.

Written by the indexing model from the issue text.

Description

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'
});
Dominant language
HTML
Stars
30
Forks
171
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sequelize/website

All issues in sequelize/website

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.