New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted primary key when self referencing through #1891

Closed
alekbarszczewski opened this Issue Jun 12, 2014 · 2 comments

Comments

3 participants
@alekbarszczewski
Contributor

alekbarszczewski commented Jun 12, 2014

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?

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Jun 12, 2014

Hmm, afair it should be created a primary key from both columns. Def. a bug.

@mickhansen mickhansen added the Bug label Jun 12, 2014

@janmeier janmeier closed this in df27974 Jul 1, 2014

@janmeier

This comment has been minimized.

Member

janmeier commented Jul 1, 2014

Fixed in df27974 - both fields are now used for the primary key

If you mean to remove the generated key completely, you should explicitly add a primary key field to your Followings model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment