Sequelize, foreign keys as composite primary key

Sergio Flores

it is possible to define two foreign keys as a composite primary key of a model?

A user can only be a member of one family, a family can have many members and the family-members table need the references of the user and family

const User = sequelize.define(
    'User',
    {
        id: { type: dataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true },
        name: { type: dataTypes.STRING(30) },
        email: { type: dataTypes.STRING(30) }
        ...
    },
    {
        classMethods: {
            associate(models) {
                User.hasOne(models.FamilyMember, {
                    foreignKey: 'user_id'
                }
            }
        }
    }
)

const Family = sequelize.define(
    'Family',
    {
        name: { type: dataTypes.STRING(30) }
    },
    {
        classMethods: {
            associate(models) {
                Family.hasMany(models.FamilyMember, {
                    foreignKey: 'family_id'
                }
            }
        }
    }
)

const FamilyMember = sequelize.define(
    'FamilyMember',
    {
        name: { type: dataTypes.STRING(30) },
        /*
        family_id and user_id will be here after associations but I wanted them to be a composite primaryKey
        */
    }
)
Sergio Flores

In fact, almost I got the solution from documentation:

User = sequelize.define('user', {});
Project = sequelize.define('project', {});
UserProjects = sequelize.define('userProjects', {
    status: DataTypes.STRING
});

User.belongsToMany(Project, { through: UserProjects });
Project.belongsToMany(User, { through: UserProjects });

By default the code above will add projectId and userId to the UserProjects table, and remove any previously defined primary key attribute - the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns.

Source

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sequelize foreign key reference composite primary key

Foreign key relationship with composite primary keys in mysql

Spring save composite primary key with foreign keys

Composite primary key of two foreign keys

Entity mapping for one to many with multiple foreign keys as composite primary key

Entity Framework - Composite Primary Key formed by 3 Foreign Keys

Hibernate Mapping composite primary key that contains foreign keys

Is possible to have a Composite Primary Key which components are entity foreign keys?

Composite foreign key as primary key

JPA Composite Foreign Primary keys

Mapping a composite foreign key to a composite primary key

EntityFramework - Composite keys and foreign key

JPA / Hibernate - Composite primary key with foreign key

Hibernate foreign key with a part of composite primary key

JPA & Hibernate - Composite primary key with foreign key

JPA & Hibernate - Composite primary key with foreign key

JPA composite primary key/foreign key mapping

SQL: Foreign key references a composite primary key

Room composite Primary Key link to Foreign Key

Foreign Key Reference Composite Primary Key

Using Composite Primary Key As Foreign Key

One of Composite primary key as Foreign key Mysql

Composite primary key vs multiple primary keys

How to consider one key among composite primary keys in a table as a foreign key in a another table in postgres

Mapping composite primary and foreign keys with JPA

Composite primary key in junction table - Sequelize

Two foreign keys as primary key

Hibernate: Where do insertable = false, updatable = false belong in composite primary key constellations involving foreign keys?

best solution for Django Composite primary-key from foreign-keys