Updating Sequelize Major Version (Or Not)
Daily Standup
April 13, 2019
Working on GFT after a work-trip break. Before diving in too deep I decided to upgrade Sequelize from version 4 to 5 on the project. There were some breaking changes which I became acquainted with š In the end I decided to delay the upgrade and instead focus on making progress in the app itself.
Default Column Generation
The immediately apparent change was how column names are auto-generated now, depending on whether underscored: true
is defined on models. It used to be that if you set underscored
to true
, the column names are generated in an underscored form (this applies to default columns like created_at
, updated_at
, etc.). In Sequelize 5 theyāve changed this to make auto-generated column names go in in camelCase with the field set to the underscored name. The behavior is defined here.
Apparently this is normal behavior on other ORMs, and I can see how it makes sense by reading some of the comments in the discussion. But my models werenāt set up in this way, and the result is that the model queries are changed significantly:
# SEQUELIZE 4 |
This prompted some NOT NULL
violation errors, since the query was looking for fields like createdAt
which donāt exist in my models. The solution is to define these auto-generated fields on every model:
const LocalUser = sequelize.define('LocalUser', { ... }, |
This got the queries back to what I expected:
# SEQUELIZE 5 WITH createdAt updatedAt fields defined |
So then I would need to add this to all my model definitions and migration files.
An alternative way forward could have been accept the camelCase auto-defined column names, and maybe there would be less changing to do, and it would make my model files less verbose. Though not sure how this would affect other areas of the model definitions without looking into this further.
Association Queries
The new version also changed how model associations are queried:
# SEQUELIZE 4 |
ā¦prompting the following error in Sequelize 5:
Unhandled rejection SequelizeDatabaseError: column Venue.country_id does not exist |
I did not get into this fix.
Model Definitions
There was another big change, in how models are defined:
// SEQUELIZE 4 |
I havenāt looked deeply into this Model class to understand this change, and decided that doing so (now) and re-doing all of the models in the new syntax could potentially take a looooot of time, time Iād really rather spend building out a working prototype of the app with all of the features I want in a proof-of-concept. So ultimately I decided to revert back to Sequelize 4 and save this upgrade for another day.
Define Model Attributes on Global Level
Not related to the version change, but I found a code snippet to define model attributes on a global level, instead of in each model as I have been doing up to now:
module.exports = { |
I think I will implement this when I do do the upgrade.
Other Stuff
When the time does come to upgrade, hereās a resource that may prove helpfulā¦it describes how to use classes for model definition, before this was made the m.o. in Sequelize 5.
Up Next
Working on the model that I couldnāt get to work before the migrations detourā¦wish me luck!