Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11601

How do I push serveral id's from one table to another in sequelize?

$
0
0

I am trying to create a booking system using sequelize, and I have to seperate tables.One for Rooms and one for Hotel

What I want to do, is basically to push the ID of the room into the hotel table column called rooms.

It should be able to hold more than one id. Ergo an array. I am using myql which means I have define the rooms column as json as I understand it.

I can push only one id but that as I said is not enough.

My room model and controller looks like this

const Room = (sequelize, Sequelize) => {  const Room = sequelize.define("rooms", {    title: {      type: DataTypes.STRING,      allowNull: false,    },    price: {      type: DataTypes.INTEGER,      allowNull: false,    },    maxPeople: {      type: DataTypes.INTEGER,      allowNull: false,    },    desc: {      type: DataTypes.TEXT,      allowNull: false,    },    roomNumbers: {      type: DataTypes.JSON,      allowNull: true,    },  });  return Room;};export default Room;export const createRoom = async (req, res, next) => {  const hotelId = req.params.hotelid;  const newRoom = new Room({    title: req.body.title,    price: req.body.price,    maxPeople: req.body.maxPeople,    desc: req.body.desc,    roomNumbers: {      number: req.body.roomNumbers.number,      unavailableDates: [req.body.roomNumbers.unavailableDates],    },  });  try {    const savedRoom = await newRoom.save();    try {      await Hotel.findOne({        where: {          id: hotelId,        },      }).then((Hotel) => {        Hotel.update({          rooms: savedRoom.id,        });      });    } catch (err) {      next(err);    }    res.status(200).json(savedRoom);  } catch (err) {    next(err);  }};

As you can see it pushes the savedRoom id but only the single one.

Thanks in advance!


Viewing all articles
Browse latest Browse all 11601

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>