Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Foreign key by default is null

Đang mở
#607 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
45/100
Loại issue
Tài liệu
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
sqlite, typescript
Lĩnh vực
documentation

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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'
});
Ngôn ngữ chính
HTML
Star
30
Fork
171
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của sequelize/website

Tất cả issue của sequelize/website

Issue tương tự

Thêm issue về Documentation

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.