EDIT: 7 minutes after posting this, ofcourse i find the solution by simply logging 'models' in the model. Yes, my table is called 'posters' and the model 'Poster'. But the models variable holds the table name. RESOLVED
EDIT2: So I received a notification that there was a reply and my answer isn't very clear. Just to make sure it's clear for you guys, I will clarify it a bit:
So in my database I had my table named 'posterParts', so first letter lower case and plural. My model (schema/models) for Sequelize was named 'PosterPart' with the first letter capital and singular. in the code I was calling the 'PosterPart' (models.PosterPart) but I had to call the name defined in the database, so models.posterParts. This is more a mistake on my side and I would suggest naming your database tables the same as you name your models to not run in problems like mine.
----- Original question:
I'm trying to get sequelize working with GraphQL in an express server, but I'm running into a problem. Sequalize on its own is working perfectly, but now I found this example to seperate models in seperate files. Of course I want that!
I tried to implement it exactly as it's done in this example, but it fails at this piece in models/index.js:
Object.keys(db).forEach((modelName) => {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}
});
with the following error:
(...)/node_modules/sequelize/lib/associations/mixin.js:12
if (!target.prototype || !(target.prototype instanceof this.sequelize.Model)) {
^
TypeError: Cannot read property 'prototype' of undefined
Since it uses the 'associate' from the model, I will include the setup of my model too:
Now after a lot of debugging I initially thought using es6 syntax might be breaking it, so I tried to change all es6 syntax to be exactly as in this example, but it's still breaking. I get the following error:
I also thought that maybe the foreignKey: 'id' was causing issues, but that also don't seem to be the case.
Am I missing something obvious or is this a bug? I will revert to just using one big file for all models for now. But I would like to implement seperate models in the future, when the project is getting bigger.
EDIT: 7 minutes after posting this, ofcourse i find the solution by simply logging 'models' in the model. Yes, my table is called 'posters' and the model 'Poster'. But the models variable holds the table name. RESOLVED
EDIT2: So I received a notification that there was a reply and my answer isn't very clear. Just to make sure it's clear for you guys, I will clarify it a bit: So in my database I had my table named 'posterParts', so first letter lower case and plural. My model (schema/models) for Sequelize was named 'PosterPart' with the first letter capital and singular. in the code I was calling the 'PosterPart' (
models.PosterPart
) but I had to call the name defined in the database, somodels.posterParts
. This is more a mistake on my side and I would suggest naming your database tables the same as you name your models to not run in problems like mine.----- Original question: I'm trying to get sequelize working with GraphQL in an express server, but I'm running into a problem. Sequalize on its own is working perfectly, but now I found this example to seperate models in seperate files. Of course I want that!
I tried to implement it exactly as it's done in this example, but it fails at this piece in
models/index.js
:with the following error:
Since it uses the 'associate' from the model, I will include the setup of my model too:
Now after a lot of debugging I initially thought using es6 syntax might be breaking it, so I tried to change all es6 syntax to be exactly as in this example, but it's still breaking. I get the following error:
I also thought that maybe the
foreignKey: 'id'
was causing issues, but that also don't seem to be the case.Am I missing something obvious or is this a bug? I will revert to just using one big file for all models for now. But I would like to implement seperate models in the future, when the project is getting bigger.