【发布时间】:2018-07-02 17:42:28
【问题描述】:
我正在使用 sequelize 和 mssql。
我尝试创建一对一关系like in this picture:
所以我使用了 sequelize docs 中的代码:
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]';
问题依然存在。
【问题讨论】:
标签: javascript sql node.js database sequelize.js