Sequelize joined table column names for BelongsToMany association
Sequelize joined table column names for BelongsToMany association
我正在为我的 postgres 数据库使用 sequelize,我有以下两个 table:
- 艺术家
- 专辑
它们之前是使用 sequelize.define
定义的
这是一种M:N关系,所以我决定采用以下关联:
Artist.belongsToMany(Albums, { through: 'ArtistAlbums' }
Album.belongsToMany(Artist, { through: 'ArtistAlbums' }
这很好用,但是 table ArtistAlbums 后来的列名很奇怪:
"ArtistArtistId","AlbumAlbumId"指的是艺术家(artistId)和专辑(albumId)的主键。
但是我只想在那里有“albumId”和“artistId”,但不知道如何告诉 sequelize 这样做。我尝试了以下方法:
- 试图在 through 选项中指定一个模型,该模型指定了两个类似的键。
问题:我的密钥和生成的密钥都在 table
- 我试过给它添加
unique: false
选项,也没有用。
- 我尝试使用“as”和“sourceKey”、“targetKey”以及“uniqueKey”,但均无济于事。
感谢任何帮助!
编辑 1:
我认为使用 otherKey 我能够将其中一列设置为我想要的名称,但另一列不行。
试试这个:
Artist.belongsToMany(Album, {
through: 'ArtistAlbums',
unique: false,
foreignKey: 'artistId'
});
Album.belongsToMany(Artist, {
through: 'ArtistAlbums',
unique: false,
foreignKey: 'albumId'
});
请注意,我的代码与您的代码之间的唯一区别 是foreignKey
定义。
上面的代码在我的 postgreSQL 数据库中创建了以下结果。 (我省略了一些数据):
CREATE TABLE public."ArtistAlbums"
(
"createdAt" timestamp with time zone NOT NULL,
"updatedAt" timestamp with time zone NOT NULL,
"artistId" integer NOT NULL,
"albumId" integer NOT NULL,
CONSTRAINT "ArtistAlbums_pkey" PRIMARY KEY ("artistId", "albumId"),
CONSTRAINT "ArtistAlbums_albumId_fkey" FOREIGN KEY ("albumId")
REFERENCES public."Album" (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT "ArtistAlbums_artistId_fkey" FOREIGN KEY ("artistId")
REFERENCES public."Artist" (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
生成我使用的其他表格:
const Artist = sequelize.define(
'Artist',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true
}
},
{
freezeTableName: true
}
);
const Album = sequelize.define(
'Album',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true
}
},
{
freezeTableName: true
}
);
我正在为我的 postgres 数据库使用 sequelize,我有以下两个 table:
- 艺术家
- 专辑
它们之前是使用 sequelize.define
这是一种M:N关系,所以我决定采用以下关联:
Artist.belongsToMany(Albums, { through: 'ArtistAlbums' }
Album.belongsToMany(Artist, { through: 'ArtistAlbums' }
这很好用,但是 table ArtistAlbums 后来的列名很奇怪:
"ArtistArtistId","AlbumAlbumId"指的是艺术家(artistId)和专辑(albumId)的主键。 但是我只想在那里有“albumId”和“artistId”,但不知道如何告诉 sequelize 这样做。我尝试了以下方法:
- 试图在 through 选项中指定一个模型,该模型指定了两个类似的键。
问题:我的密钥和生成的密钥都在 table
- 我试过给它添加
unique: false
选项,也没有用。 - 我尝试使用“as”和“sourceKey”、“targetKey”以及“uniqueKey”,但均无济于事。
感谢任何帮助!
编辑 1:
我认为使用 otherKey 我能够将其中一列设置为我想要的名称,但另一列不行。
试试这个:
Artist.belongsToMany(Album, {
through: 'ArtistAlbums',
unique: false,
foreignKey: 'artistId'
});
Album.belongsToMany(Artist, {
through: 'ArtistAlbums',
unique: false,
foreignKey: 'albumId'
});
请注意,我的代码与您的代码之间的唯一区别 是foreignKey
定义。
上面的代码在我的 postgreSQL 数据库中创建了以下结果。 (我省略了一些数据):
CREATE TABLE public."ArtistAlbums"
(
"createdAt" timestamp with time zone NOT NULL,
"updatedAt" timestamp with time zone NOT NULL,
"artistId" integer NOT NULL,
"albumId" integer NOT NULL,
CONSTRAINT "ArtistAlbums_pkey" PRIMARY KEY ("artistId", "albumId"),
CONSTRAINT "ArtistAlbums_albumId_fkey" FOREIGN KEY ("albumId")
REFERENCES public."Album" (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT "ArtistAlbums_artistId_fkey" FOREIGN KEY ("artistId")
REFERENCES public."Artist" (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)
生成我使用的其他表格:
const Artist = sequelize.define(
'Artist',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true
}
},
{
freezeTableName: true
}
);
const Album = sequelize.define(
'Album',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true
}
},
{
freezeTableName: true
}
);