Tag: 外键

Sequelize – Mysql的外键添加约束失败与ALTER TABLE

我试图用下面的Sequelize模型创build两个表: const Sequelize = require('sequelize'); module.exports = function (db) { const Product = db.define('product', { intProductID: { type: Sequelize.INTEGER(11), primaryKey: true, autoIncrement: true, allowNull: false }, strName: { type: Sequelize.STRING }, douPrice: { type: Sequelize.DOUBLE } }); return Product; } const Sequelize = require('sequelize'); module.exports = function(db){ const User = db.define('user', { intUserID:{ type: Sequelize.INTEGER, […]

Looback:configurationacl和关系来列出所有属于logging使用的对象

我试图设置一个回环项目,我遇到了一个基本问题,我不明白。 所以我基本上有一个自定义用户模型定义与以下关系和acls: "relations": { … "projects": { "type": "hasMany", "model": "project", "foreignKey": "userId" } } "acls": [ { "accessType": "*", "principalType": "ROLE", "principalId": "$everyone", "permission": "DENY" }, { "accessType": "READ", "principalType": "ROLE", "principalId": "$owner", "permission": "ALLOW" } ] 而一个定义了属性的“项目”模型: "relations": { "user": { "type": "belongsTo", "model": "user", "foreignKey": "userId" } }, "acls": [ { "accessType": […]

join连接和关系

我有一个数据库运行,但我还没有能够执行两个表之间的联接。 我已经做了另外两个表之间的连接,但是这个不起作用。 在我的db.js文件上,我build立了以下表之间的关系: db.diet.hasMany(db.meal, {as : 'Diet', foreignKey : 'dietId'}); 而在我的server.js文件,我试图build立这个查询: db.meal.findAll({ include : [db.diet], where : { dietId : idDiet } }).then(function (meals) { res.json(meals) }); 这给了我以下错误: ReferenceError: idDiet is not defined 我也尝试查询这样的饮食表: db.diet.findAll({ include :[db.meal], where : { idDiet : dietId } }).then(function (meals) { res.json(meals); }); 但是这也给我一个错误: ReferenceError: dietId is not defined […]

如何用子文档数组更新MongoDB文档

我正在使用一个MongoDB数据库,这个数据库的集合模型类 , 学生 , 主题和学术performance 。 下面是基于Mongoose的模式和模型: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var ObjectId = Schema.Types.ObjectId; // SUBJECT collection var subjectSchema = new Schema({ name : { type: String, required: true }, category : { type: String, required: true } }); var Subject = mongoose.model('Subject', subjectSchema); // STUDENT collection var studentSchema = […]

如何用外键创build带有sequelize或sequelize-cli的连接表

我正在为两种types创build模型和迁移,玩家和团队有许多关系。 我使用sequelize模型:创build,但不看如何指定外键或连接表。 sequelize model:create –name Player –attributes "name:string" sequelize model:create –name Team –attributes "name:string" 模型创build后,我添加关联。 玩家: Player.belongsToMany(models.Team, { through: 'PlayerTeam', foreignKey: 'playerId', otherKey: 'teamId' }); 团队: Team.belongsToMany(models.Player, { through: 'PlayerTeam', foreignKey: 'teamId', otherKey: 'playerId' }); 然后迁移与运行 sequelize db:migrate 有玩家和团队的表,但数据库中没有连接表(也不是外键)。 如何创build外键和连接表? 有没有关于如何做到这一点的权威指南?