Skip to content

Unwanted primary key when self referencing through #1891

@alekbarszczewski

Description

@alekbarszczewski

Tested on master:

var Sequelize = require('sequelize')
var pg = require('pg')

var sequelize = new Sequelize(process.env.DATABASE_URL || 'postgres://postgres:postgres@localhost/sequelize-test')

var User = sequelize.define('User',{})
var Following = sequelize.define('Following',{})

User.hasMany(User,{ through: Following, as: 'Follower', foreignKey: 'FollowerId' })
User.hasMany(User,{ through: Following, as: 'Followed', foreignKey: 'FollowedId' })

sequelize.sync({ force: true }).success(function(){
    console.log('done');
});

This executes following SQL code according to Following model:

CREATE TABLE "Followings" (
    "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL
    ,"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL
    ,"FollowedId" INT REFERENCES "Users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
    ,"FollowerId" INT REFERENCES "Users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
    ,UNIQUE (
        "FollowedId"
        ,"FollowerId"
        )
    ,PRIMARY KEY ("FollowerId")
    )

As you can see there is PRIMARY KEY ("FollowerId") - and it shouldnt be there... How I can force Sequelize not to add this PK?

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions