我正在使用sequelize和postgresql创建一个节点应用程序,并陷入了这个问题。我在sequelize中有三种型号: BoxTemplate
BoxTemplateWorkspace Workspace
模型 BoxTemplateWorkspace
是一个多对多的关联,链接了许多 BoxTemplate
对许多人来说 Workspace
. 我试图做的是在 BoxTemplate
模型,将my boxtemplate中的列标题与my boxtemplateworkspace中的列工作空间代码互连。
我已经知道如何使用普通列和驻留在同一表/模型中的fk列创建复合唯一约束。但我没办法挺过去。
吼叫我的代码,以获得更好的理解
boxtemplate.js
var boxTemplate = sequelize.define('BoxTemplate', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: title_WorkspaceCode_unique
},
Title: {
type: DataTypes.STRING(150),
allowNull: false,
unique: true
},
//... more columns
}, {
indexes: [{
unique: 'title_WorkspaceCode_unique',
fields: ['Title', /*here should fit the WorkspaceCode present in BoxTemplateWorkspace*/]
}],
createdAt: 'CreatedDate',
updatedAt: 'LastUpdate',
timestamps: true,
underscored: false,
freezeTableName: true,
});
boxTemplate.associate = models => {
// model that makes a many to many relationship
boxTemplate.hasMany(models.BoxTemplateWorkspace, {foreignKey: 'BoxTemplateCode'});
}
return boxTemplate;
};
boxtemplateworkspace
var boxTemplateWorkspace = sequelize.define('BoxTemplateWorkspace', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
IsActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false
},
InactivatedDate: {
type: DataTypes.DATE,
},
},{
timestamps: false,
underscored: false,
freezeTableName: true
});
boxTemplateWorkspace.associate = models => {
boxTemplateWorkspace.belongsTo(models.BoxTemplate, { foreignKey: 'BoxTemplateCode' });
// column that I want to use as a composite unique constraint
boxTemplateWorkspace.belongsTo(models.Workspace, { foreignKey: 'WorkspaceCode' });
boxTemplateWorkspace.belongsTo(models.UserSK, { foreignKey: 'InactivatedByUserCode' });
}
return boxTemplateWorkspace;
};
正如这里所建议的,usersk和workspace模型
usersk.js
var userSk = sequelize.define('UserSK', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
Email: {
type: DataTypes.STRING(320),
allowNull: false,
unique: true
},
UserName: {
type: DataTypes.STRING(150),
allowNull: false,
unique: true
},
}, {
createdAt: 'CreatedDate',
updatedAt: 'LastUpdate',
timestamps: true,
underscored: false,
freezeTableName: true
});
userSk.associate = models => {
userSk.hasMany(models.BoxTemplate, {foreignKey: 'CreatedByUserCode'});
userSk.hasMany(models.BoxTemplate, {foreignKey: 'UpdatedByUserCode'});
}
return userSk;
workspace.js
var workspace = sequelize.define('Workspace', {
ID: {
type: DataTypes.UUIDV4,
defaultValue: sequelize.literal('gen_random_uuid()'),
allowNull: false,
primaryKey: true,
unique: true
},
InvitedDate: {
type: 'TIMESTAMP',
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
allowNull: true
},
WorkspaceName: {
type: DataTypes.STRING(100),
allowNull: true
}
}, {
timestamps: false,
underscored: false,
freezeTableName: true
});
workspace.associate = models => {
workspace.hasMany(models.BoxTemplateWorkspace, {foreignKey: 'WorkspaceCode'});
}
return workspace;
};
暂无答案!
目前还没有任何答案,快来回答吧!