【发布时间】:2020-07-09 05:43:46
【问题描述】:
我已经定义了以下表格、存档和信息。每个文件可以有许多信息标签。字段和标签的组合是独一无二的。为此,我需要一个由字段和标签组成的复合主键。 field 是指归档表中的字段。模型定义如下。
存档表:
module.exports = (sequelize, DataTypes) => {
let archive = sequelize.define('archive', {
fileid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
filename: {
type: DataTypes.STRING,
unique: false,
allowNull: false
},
originalname: {
type: DataTypes.STRING,
unique: false,
allowNull: false
},
downloadlink: {
type: DataTypes.STRING,
unique: false,
allowNull: false
},
domain: {
type: DataTypes.STRING,
unique: false,
allowNull: false
},
sem: {
type: DataTypes.INTEGER,
unique: false,
allowNull: false
},
branch: {
type: DataTypes.STRING,
unique: false,
allowNull: false
}
});
archive.associate = models => {
models.archive.hasMany(models.info, {
foreignKey: 'fileid'
});
models.archive.hasMany(models.upvotes, {
foreignKey: 'fileid'
});
};
return archive;
};
信息表
module.exports = (sequelize, DataTypes) => {
let info = sequelize.define('info', {
tag: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true
}
});
info.associate = models => {
models.info.belongsTo(models.archive, {
foreignKey: 'fileid',
primaryKey: true
});
};
return info;
};
制作 primaryKey: true 不起作用。我已经尝试过了:也是。我似乎无法让它工作。
【问题讨论】:
标签: node.js express sequelize.js