Sequelize association creating extra column

Viewed 60

just started using sequelize and I was trying to setup a foreign key using the association functions. I have 2 models:

User:

const User = sequelize.define("User", {
        userId: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            allowNull: false,
            unique: true,
            primaryKey: true
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true,
            validate: {
                isEmail: true,
                notNull: true,
                notEmpty: true
            }
        },
        userName: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                notNull: true,
                notEmpty: true
            }
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                notNull: true,
                notEmpty: true
            }
        },
        profileImage: {
            type: DataTypes.STRING(300),
            validate: {
                isUrl: true
            }
        },
        rating: {
            type: DataTypes.DECIMAL(10, 2),
            validate: {
                isDecimal:true
            }
        }
    },{
        tableName:"user"
    }
);

Post:

const Post = sequelize.define("Post", {
        postId: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            allowNull: false,
            unique: true,
            primaryKey: true
        },
        title: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
        content: {
            type: DataTypes.TEXT,
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: "OPEN",
            allowNull: false,
            validate: {
                notEmpty: true
            }
        }
    }, {
        tableName: "post"
    }
);

I am currently setting the foreign key like this in my resetTables.js:

User.hasMany(Post);
Post.belongsTo(User, {
    foreignKey: {
        name: "userId",
        type: DataTypes.UUID,
        allowNull: false,
        validate: {
            notNull: true
        }
    },
    onDelete: "CASCADE"
});

But for some reason I keep getting an extra column being made for the foreign key:

Executing (default): CREATE TABLE IF NOT EXISTS "post" ("postId" UUID NOT NULL UNIQUE , "title" VARCHAR(255) NOT NULL, "content" TEXT NOT NULL, "status" VARCHAR(255) NOT NULL DEFAULT 'OPEN', "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, "userId" UUID NOT NULL REFERENCES "user" ("userId") ON DELETE CASCADE ON UPDATE CASCADE, "UserUserId" UUID REFERENCES "user" ("userId") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY ("postId"));

as you can see I am getting my "userId" column created correctly but for some reason another column called "UserUserId" is being created as well? I have other associations that have been defined similarly and only this table is having this issue. Any help would be appreciated.

I am using Sequelize V6 with NodeJS and PostgreSQL on ElephantSQL.

1 Answers

I know i'm late to the party but i had the same issue and found out that it's because of belongTo, you only need to use hasMany when declaring 1:N associations.

Related