Sequelize 关联创建额外列

问题描述 投票:0回答:3

刚刚开始使用sequelize,我尝试使用关联函数设置外键。我有 2 个型号:

用户:

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"
    }
);

帖子:

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"
    }
);

我目前正在我的resetTables.js 中设置外键:

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

但由于某种原因,我不断收到为外键创建的额外列:

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"));

如您所见,我正确创建了“userId”列,但由于某种原因,还创建了另一个名为“UserUserId”的列?我还有其他类似定义的关联,只有这个表有这个问题。任何帮助将不胜感激。

我在 ElephantSQL 上使用 Sequelize V6 以及 NodeJS 和 PostgreSQL。

node.js postgresql sequelize.js
3个回答
0
投票

我知道我迟到了,但我也遇到了同样的问题,并发现这是因为

belongTo
,你只需要在声明1:N关联时使用
hasMany


0
投票

您必须在

foreignKey
函数中定义
hasMany
。查看文档。这是正确的代码。

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

0
投票

我遇到了这个问题。经过大量努力,我通过使用

belongsTo
hasMany
更改关联来解决问题。是
hasOne
hasMany

© www.soinside.com 2019 - 2024. All rights reserved.