Foreign key by default is null
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sequelize/website
-
Difficulty 1/5 1-3 hours Newbie friendliness 74/100
-
Update Migration.mdOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Error: Only HasMany associations support include.separateMay be free again @SippieCup claimed this 187 days ago, and no pull request is open. Open
-
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
All issues in sequelize/website
Similar issues
-
copilot documentation
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
external-issue to-triage
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 92/100
transitmatters/mbta-slow-zone-bot#70 ·
Maintainers usually reply within 1 day
-
skill:code-review
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day