Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为调度应用程序续集模型关联_Javascript_Node.js_Postgresql_Sequelize.js - Fatal编程技术网

Javascript 为调度应用程序续集模型关联

Javascript 为调度应用程序续集模型关联,javascript,node.js,postgresql,sequelize.js,Javascript,Node.js,Postgresql,Sequelize.js,我正在开发一个应用程序,用于处理大型童子军营地的节目安排。我关注的主要模型有: 程序代表单个程序活动(例如皮划艇、山地自行车等)。此模型上有一系列字段,所有字段都与此问题无关 var Program = sequelize.define("Program", { name: { type: DataTypes.STRING, allowNull: false }, max_participants_per_period: DataTypes.INT

我正在开发一个应用程序,用于处理大型童子军营地的节目安排。我关注的主要模型有:

程序代表单个程序活动(例如皮划艇、山地自行车等)。此模型上有一系列字段,所有字段都与此问题无关

var Program = sequelize.define("Program", {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    max_participants_per_period: DataTypes.INTEGER,
    [...bunch of other fields...]
  }, {
    underscored: true,
    classMethods: {
      associate: function(models) {
        Program.hasMany(models.ProgramPeriod);
      }
    }
});
var Unit = sequelize.define("Unit", {
    unit_number: {
      type: DataTypes.STRING,
      unique: true
    },
    number_of_youth: DataTypes.INTEGER,
    number_of_leaders: DataTypes.INTEGER,
  }, {
    classMethods: {
      associate: function(models) {
        Unit.belongsToMany(models.ProgramPeriod, { through: 'Schedule', foreignKey: 'unit_id' });
      }
    },
    getterMethods: {
      total_participants: function() {
        return this.number_of_youth + this.number_of_leaders;
      }
    }
  });
每个节目都安排在一个节目周期中。节目周期有开始和结束时间。不同的节目有不同的持续时间(有些是半天,有些是全天,等等)

一个单位是一组侦察兵和领导。一个单位在一个计划期内将被安排到任何数量的计划中。还有许多其他领域与这个问题无关

var Program = sequelize.define("Program", {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    max_participants_per_period: DataTypes.INTEGER,
    [...bunch of other fields...]
  }, {
    underscored: true,
    classMethods: {
      associate: function(models) {
        Program.hasMany(models.ProgramPeriod);
      }
    }
});
var Unit = sequelize.define("Unit", {
    unit_number: {
      type: DataTypes.STRING,
      unique: true
    },
    number_of_youth: DataTypes.INTEGER,
    number_of_leaders: DataTypes.INTEGER,
  }, {
    classMethods: {
      associate: function(models) {
        Unit.belongsToMany(models.ProgramPeriod, { through: 'Schedule', foreignKey: 'unit_id' });
      }
    },
    getterMethods: {
      total_participants: function() {
        return this.number_of_youth + this.number_of_leaders;
      }
    }
  });
图表:

┌────────────────┐                                  ┌─────────────────────────┐                
│                │                                  │      ProgramPeriod      │                
│    Program     │                                ╱│       pk: id(INT)       │                
│  pk: id(INT)   │─Program.hasMany(ProgramPeriod)───│   fK: program_id(INT)   │                
│                │                                ╲│ references Programs.id  │                
└────────────────┘                                  │                         │                
                                                    └─────────────────────────┘                
                                                                ╲│╱                            
                                                                  │                             
                                                                  │                             
                                                                  │                             
                                     ProgramPeriod.belongsToMany(Unit, { through: Schedule })  
                                                                  │                             
                                                                  │                             
                                                                  │                             
                                                                ╱│╲                            
                                                    ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐                

                                                    │        Schedule         │                
                                                              unit_id                          
                                                    │    program_period_id    │                

                                                    └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘                
                                                                ╲│╱                            
                                                                  │                             
                                                                  │                             
                                                                  │                             
                                     Unit.belongsToMany(ProgramPeriod, { through: Schedule })  
                                                                  │                             
                                                                  │                             
                                                                  │                             
                                                                ╱│╲                            
                                                    ┌─────────────────────────┐                
                                                    │                         │                
                                                    │          Units          │                
                                                    │       pk: id(INT)       │                
                                                    │                         │                
                                                    │                         │                
                                                    └─────────────────────────┘                
通过此设置,我可以查询分配给
程序
的所有
单元

Program.find({ 
    where: { id: 1 }, 
    include: [{ model: models.ProgramPeriod, 
                include: [ models.ProgramPeriod.associations.Schedule ]  
             }]
});
…我可以查询
单元
程序时间表
s:

Unit.find({ 
    where: { id: 174 }, 
    include: [{ model: models.ProgramPeriod, 
                include: [ models.ProgramPeriod.associations.Program] 
             }] 
})
我在计算调度所需的一些内容时遇到困难。假设单元id 174有10名参与者。对于任何给定的
程序
,我需要查询所有
程序周期
,这些周期都有足够的可用空间,并且不与单元调度上已有的任何其他
程序周期
时间冲突

我与上述任何一种联系都没有关系;如果这能让我更容易做到,我可以改变周围的情况


谢谢!

有趣的问题,我想了很多方法来实现这一点,比如过滤整个查询等等……但由于它们没有一点有效性,我不会添加它们作为答案。期待看到一个好的答案。