Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize association creating extra column

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.

like image 602
Gareth T. Avatar asked Apr 21 '26 05:04

Gareth T.


1 Answers

You have to define foreignKey in hasMany function. Check out the documentation. Here is the correct code.

User.hasMany(Post, {
  foreignKey: {
    name: "userId",
    type: DataTypes.UUID,
    allowNull: false,
    validate: {
      notNull: true
    }
  },
  onDelete: "CASCADE"
});
Post.belongsTo(User); // No need to specify foreignKey here
like image 108
Hassan Rehan Avatar answered Apr 23 '26 20:04

Hassan Rehan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!