gpt4 book ai didi

reactjs - Sequelize JOIN 查询在获取请求时抛出以下错误

转载 作者:行者123 更新时间:2023-12-03 22:21:14 25 4
gpt4 key购买 nike

我的 'sequelize' JOIN 查询抛出以下错误。我想为“当前日期”的所有用户的表中的以下列创建一个 JOIN 查询。有时 dailystatus 列可能没有当前日期的数据。
以下是 useravailability 表中的列:
来自用户表的 photo, position
来自可用性表的 dailystatus
在给出以下查询条件时,我收到状态码为 500 的请求失败。

const userSchema = require('./server/models/user');
const availabilitySchema = require('./server/models/availability');

const UserModel = userSchema(sequelize, DataTypes);
const Availability = availabilitySchema(sequelize, DataTypes);



app.get('/service/availability', async (req, res) => {
try {
var today = moment().format('YYYY/MM/DD');
const dailyStatus = await Availability.findAll({
where: {
dailystatus: ['in', 'out']
},
include: [{
model: UserModel,
attributes: ['photo', 'position'],
where :{
createdAt : today
}
}],
});
res.status(200).json({ dailyStatus });
} catch (e) {
res.status(500).json({ message: e.message });
}

});
availability
module.exports = function(sequelize, DataTypes) {
return sequelize.define('availability', {
email: {
type: DataTypes.STRING(100),
allowNull: false

},
dailystatus: {
type: DataTypes.STRING(50),
allowNull: false
},
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
}

},{
tableName: 'availability'
});
}
user.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user', {
name: {
type: DataTypes.STRING(30),
allowNull: false
},
email: {
type: DataTypes.STRING(100),
allowNull: false

},
phonenumber: {
type: DataTypes.STRING(50),

},
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
password: {
type: DataTypes.STRING(50),
allowNull: false
},
privilege: {
type: DataTypes.STRING(100),

},
photo: {
type: DataTypes.STRING(300),

},
position: {
type: DataTypes.STRING(100),

},
dailystatus: {
type: DataTypes.STRING(50),

}
}, {
tableName: 'user'
});
};
enter image description here
enter image description here

最佳答案

可用性模型中的关联可以是:
'使用严格';

module.exports = (sequelize, DataTypes) => {
const availability = sequelize.define('availability', {
email: {
type: DataTypes.STRING(100),
allowNull: false

},
dailystatus: {
type: DataTypes.STRING(50),
allowNull: false
},
user_id: {
type: DataTypes.INTEGER(18)
},
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
}
}, {
tableName: 'availability'
});
availability.associate = function (models) {
// associations can be defined here
availability.belongsTo(models.user, {
foreignKey: "user_id",
onDelete: "CASCADE"
});
};
return availability;
};
用户模型中的关联可以是:
'use strict';
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define('user', {
name: {
type: DataTypes.STRING(30),
allowNull: false
},
email: {
type: DataTypes.STRING(100),
allowNull: false

},
phonenumber: {
type: DataTypes.STRING(50),

},
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
password: {
type: DataTypes.STRING(50),
allowNull: false
},
privilege: {
type: DataTypes.STRING(100),

},
photo: {
type: DataTypes.STRING(300),

},
position: {
type: DataTypes.STRING(100),

},
dailystatus: {
type: DataTypes.STRING(50),

}
}, {
tableName: 'user'
});
user.associate = function (models) {
// associations can be defined here
user.hasOne(models.availability, {
foreignKey: "user_id",
onDelete: "CASCADE"
});
};

return user;
};

关于reactjs - Sequelize JOIN 查询在获取请求时抛出以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62508949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com