sql - Fetch data from postgresql table missing in another table using Sequelize -
i tried retrieving data postgres sql table missing in table received sequelizeloadingerror
.
i trying retrieve list of books has not been rented particular user.
book controller
getbooks(req, res) { const userid = req.params.userid; return book .findall({ where: sequelize.literal('rentedbook.userid null'), include: [ { model: rentedbook } ] }) .then((books) => { if (books.length < 1) { res.status(400).send({ message: 'there no book in database' }); } else { res.status(201).send(books); } }) .catch(error => res.status(404).send(error)); },
book model
export default (sequelize, datatypes) => { const books = sequelize.define('book', { title: { type: datatypes.string, allownull: datatypes.false, }, isbn: { type: datatypes.text, required: true, }, total: { type: datatypes.integer, required: true }, prodyear: datatypes.string, catid: datatypes.integer, cover: datatypes.string, author: datatypes.string, description: datatypes.text }, { classmethods: { associate: (models) => { // associations can defined here books.hasmany(models.rentedbook, { foreignkey: 'bookid' }); books.hasone(models.category, { foreignkey: 'catid', ondelete: 'cascade' }); } } }); return books; };
rented book model
export default (sequelize, datatypes) => { const rentedbook = sequelize.define('rentedbook', { bookid: datatypes.integer, userid: datatypes.integer, toreturndate: datatypes.date, returndate: datatypes.date, returned: { type: datatypes.boolean, defaultvalue: false } }, { classmethods: { associate: (models) => { // associations can defined here rentedbook.belongsto(models.book, { foreignkey: 'bookid', ondelete: 'cascade' }); rentedbook.belongsto(models.user, { foreignkey: 'userid', ondelete: 'cascade' }); } } }); return rentedbook; };
i appreciate me fetch books exist in book table has not been rented before user.
Comments
Post a Comment