Reputation: 1699
I have been trying to build a simple 'messages' app.. After giving MongoDB a try, i found out its really best to do with MySQL.
So but instead of writing all tables / queries etc by hand, I want to learn building it from the ground up with an ORM, a.k. Sequelize.
Lets say I have a user table:
var User = sequelize.define('user', {
email: {
type: Sequelize.STRING,
validate: {
isEmail: true
//msg: "Email is invalid"
}
}
}
And a message table:
var Message = sequelize.define('message', {
message: {type: Sequelize.STRING},
sender: {type: Sequelize.STRING},
read: {type: Sequelize.BOOLEAN}
}
Every user can have many messages, and every message can have 2 users in the conversation.
So when i do this:
User.hasMany(Message);
Message.belongsToMany(User, {through: 'Conversation', foreign_key: 'id'});
User.sync();
Message.sync();
I would expect a 'junction table' named Conversations... Holding the user_id's.. And every message having a reference to the Conversation_id, But apparently its not true...
Does anybody know what the correct way is?
Thanks!
Upvotes: 1
Views: 144
Reputation: 29167
For create Conversations
table you need sync all: sequelize.sync();
.
Upvotes: 2