Saturday, 31 August 2013

Sequelize find primary key of table using table column name

Sequelize find primary key of table using table column name

I'm building a chat room with a messages table and a rooms table.
Rooms table:
+----------+----+
| roomname | id |
+----------+----+
| beach | 1 |
+----------+----+
| downtown | 2 |
+----------+----+
Messages table:
+----------+----------------------------------+----+--------+
| username | message | id | roomId |
+----------+----------------------------------+----+--------+
| emilio | this place is really really cool | 1 | 1 |
| moneym | this place is really really cool | 2 | 2 |
| joe | this place is really really cool | 3 | 2 |
| sandy | this place is really really cool | 4 | 1 |
+----------+----------------------------------+----+--------+
Room.hasMany(Message);
Message.belongsTo(Room);
I'm using Sequelize.js to find the room name using Room.find(). Is there a
way to pass in the room name and have the table look up the id
automatically?
Something like Message.build({username:username, message: message, roomId:
{where: {roomname: 'beach'}});
exports.getMessages = function(roomname, res) {
Room.find({where: {roomname: roomname}}).success(function(resultingRoom) {
var newMessage = Message.build({username: username, message: message,
roomId: resultingRoom.id});
newMessage.save()
});
};

No comments:

Post a Comment