Open eugene-matvejev opened 5 years ago
Thanks for the input! I will try to take a look later...
I just ran into this roadblock when using sequelize for the first time. Seeing eugenes solution helped me create my own Since you dont have to do the association exactly when you define it putting a next tick callback does the trick as it will get called before the database actually connects
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
class Conversation extends Model {
async advanceStage () {
await this.increment('stage')
}
async getCurrentQuestion () {
const Question = sequelize.models.questions.findByPk(this.question)
}
}
Conversation.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
username: {
type: DataTypes.STRING
},
stage: {
type: DataTypes.INTEGER
},
roomId: {
type: DataTypes.STRING
}
}, { sequelize });
process.nextTick(() => {
Conversation.belongsTo(sequelize.models.Campaign)
})
return Conversation
}
@megamit Your trick with nextTick
is too hacky. I don't recommend this...
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
What was unclear/insufficient/not covered in the documentation
I came from PHP, doctrine world, but now try understand sequlize, I struggle with associations, I tried to follow manual http://docs.sequelizejs.com/manual/associations.html
If possible: Provide some suggestion how we can enhance the docs
more examples
idea is to have more than one battlefield associated with each game. but only one game to each battlefield, e.g. one-to-many
to overcome it, index.js is like very needed:
it is unclear from docs, how to initiate models