gpt4 book ai didi

javascript - Sequelize 中是一对一还是多对一?

转载 作者:行者123 更新时间:2023-12-03 22:39:42 24 4
gpt4 key购买 nike

我将 sequelize 与 mssql 一起使用。

我尝试创建一对一关系 like in this picture :

所以我使用 sequelize 文档中的代码:

const Player = this.sequelize.define('player', {/* attributes */});
const Team = this.sequelize.define('team', {/* attributes */});

Player.belongsTo(Team); // Will add a teamId attribute to Player to hold the primary key value for Team

我得到了多对一 as you can see in the picture

所以我尝试这样做:
const Player = sequelize.define('player', { /* attributes */ });
const Team = sequelize.define('team', { /* attributes */ });

Player.belongsTo(Team);
Team.hasOne(Player);

结果一样。

我怎样才能建立一对一的关系?

编辑

对于此代码:
const Player = sequelize.define('player', { /* attributes */ });
const Team = sequelize.define('team', { /* attributes */ });

Player.hasOne(Team);

这就是 sequelize 所做的:
IF OBJECT_ID('[teams]', 'U') IS NOT NULL DROP TABLE [teams];
IF OBJECT_ID('[players]', 'U') IS NOT NULL DROP TABLE [players];
IF OBJECT_ID('[players]', 'U') IS NOT NULL DROP TABLE [players];
IF OBJECT_ID('[players]', 'U') IS NULL CREATE TABLE [players] ([id] INTEGER NOT NULL IDENTITY(1,1) , [createdAt] DATETIMEOFFSET NOT NULL, [updatedAt] DATETIMEOFFSET NOT NULL, PRIMARY KEY ([id]));
EXEC sys.sp_helpindex @objname = N'[players]';
IF OBJECT_ID('[teams]', 'U') IS NOT NULL DROP TABLE [teams];
IF OBJECT_ID('[teams]', 'U') IS NULL CREATE TABLE [teams] ([id] INTEGER NOT NULL IDENTITY(1,1) , [createdAt] DATETIMEOFFSET NOT NULL, [updatedAt] DATETIMEOFFSET NOT NULL, [playerId] INTEGER NULL, PRIMARY KEY ([id]), FOREIGN KEY ([playerId]) REFERENCES [players] ([id]) ON DELETE SET NULL);
EXEC sys.sp_helpindex @objname = N'[teams]';

而且问题仍然存在。

最佳答案

您应该使用 hasOne() 方法。不是 belongsTo() 就是这样:)

关于javascript - Sequelize 中是一对一还是多对一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51141251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com