You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Several bugs/questions regarding underscored/underscoredAll in Sequelize v5 #11225
P1: importantFor issues and PRs.docsFor issues and PRs. Things related to documentation, such as changes in the manuals / API reference.type: bugDEPRECATED: replace with the "bug" issue type
I am creating this issue to track them all in a single place.
I haven't looked deeply into it yet, but I believe some of them are caused mostly by a misunderstanding of how to upgrade from v4 to v5 (most likely the first thing I will do is expand on the documentation on how to do it correctly, I agree that the docs didn't go too deep into this).
I'm convinced this is not just a docs issue; this is something that should change / be improved.
Stay tuned!
Reacted by Yisi
Reacted by Gilson Souza, misos1, Matias Menker, Romain GAGNAIRE, Felipe Valtl de Mello, Nick Warabak, Maxim, Mychael Go, Kevin Rouchut, ryankevans and 3 more
Reacted by misos1, Matias Menker, Romain GAGNAIRE, Felipe Valtl de Mello, Maxim, Mychael Go, Jairo Malanay, notable-marcelo, ryankevans, ggalvez92 and 1 more
Well, the documentation is rather clear on how to use the underscored functionality, and if there is another way to use it, I would love to know.
I'm making a couple hooks today for my server to simulate the underscored functionality until this gets resolved.
I don't mind how the tables look in the database, when I use this, I want foreignId to be foreign_id when I interact with items in the database.
Reacted by misos1, David King, Sean Richards, Romain GAGNAIRE, Gilson Souza, mb8z, Tema Smirnov and T9-Forever
My solution is pretty unique to FeathersJS, the framework I'm using.
I have a before hook and an after hook. The before hook checks on a create method, if any of the keys in the objects end with "_id". If they do, they are added to a list. Then that list is checked if the key.slice(0, key.length-3) is in the list of hooks that I have manually created. If it is, then the hook is run through a camelize function.
The after hook also has a list of keys that may have the Id after them. So on find and get functions, I filter all the keys by the ones that end with "Id", then check if the key without "Id" is in the list of keys that I provided. Then I run an underscore function on the resulting keys.
It's not the best method and I really can't wait until this bug is fixed!
Reacted by misos1, Pedro Augusto de Paula Barbosa and mb8z
@frastlin Thanks a lot for the input. The bug is indeed frustrating. Cost me spending at least 2 hours of looking what I do wrong that it does not work.
Well, the documentation is rather clear on how to use the underscored functionality, and if there is another way to use it, I would love to know.
I'm making a couple hooks today for my server to simulate the underscored functionality until this gets resolved.
I don't mind how the tables look in the database, when I use this, I want foreignId to be foreign_id when I interact with items in the database.
For those stumbled upon this issue, here's how I've fixed it:
converted timestamps to lowercase manually, like this:
constModel=sequelize.define('application',{// some fields},{underscored: true,createdAt: 'created_at',// <====== this line and the following oneupdatedAt: 'updated_at',}
converted all the foreign keys to lowercase like this:
// Declaring 2 models, Parent and Child, Child model has the reference to Parent model// via 'parent_id' foreign key.Parent.hasMany(Child,{foreignKey: 'parent_id'});// note the 'foreign_key' parameterChild.belongsTo(Parent,{foreignKey: 'parent_id'});// and here as well
Reacted by Surface Finish Technologies, inc., Felipe Valtl de Mello, Jason Bodily, Philip Bleach, atzcl, David Oliveira, Rômulo Rocha de Oliveira, Daniel de Paula, Igor Antonov, Cedric Simon and 9 more
Reacted by Ruki, Felipe Valtl de Mello, atzcl, Rômulo Rocha de Oliveira, Osman Pontes, Daniel de Paula, Gustavo Silveira and Webdzo Technologies
Just a note that you still have to assign a "foreignKey" value and explicitly name your underscored value as of v6.3.3. Otherwise, findAll will return both your underscored version and a camel case version.
Reacted by Alex V. Povar, Sebastian Fratini and CJ Thompson
In addition to what @joshparolin is saying, that is still not working on some cases. I have all my foreign keys specified as snake_case and when i try to run a setXXX with a many-to-many relationship it fails but only when I am trying to add new records to the join table. Not if I try to delete.
Eg. if the community has features [1, 2, 3] and I run setFeatures([1,2]) it works. But if the community has [1, 2, 3] and I run setFeatures([3, 4, 5]) it breaks.
As you can see, the instance has both camelCase and snake_case but in this case only one of them has the right values so the entire query is failing.
My goal is to use snake_case on the DB but camelCase on my GraphQL API.
My solution was to actually point my join table model foreignKeys to the camelCase that I defined on the model. While I can see why this works, these are some manual steps I don't think I should be doing when all the information for the fields is available:
Community.belongsToMany(DdFeature, {
as: 'features',
through: CommunityFeature,
foreignKey: 'idCommunity', //This is the field I define in the model
otherKey: 'idDdFeature', ///This is the field I define in the model
onDelete: 'RESTRICT',
onUpdate: 'CASCADE'
});
since it's been over than a year with this issue, def should be added in the documentation as a known issue under the "Upgrade to V6/V5" section. @papb
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
That depends on what your issue is, since this issue regroups a bunch of them.
I'm actually going to close this issue as we have no idea which bug has been fixed and which bug remains.
If you're still experiencing an issue, please either comment in the relevant closed issue (listed in the first post of this thread) that you're still experiencing that issue and it should be re-opened, or open a new bug report.
What a surprise for people who need to update some old projects with the newer version of sequelize... What if you have zillions of models and _ is used everywhere in the codebase (and it's not typescript). Do you have to search and fix it manually? This option is required for backwards compatibility.
Reacted by Maxim, plr, Dmytro Parzhytskyi, Rovack, Jacob Beard and Romain GAGNAIRE
This is very sad. 5 years now and still not fixed. I'm using "sequelize": "^6.37.3" and "sequelize-cli": "^6.6.2" and my timestamps are still in camelCase. I used sequelize-cli passing the field names in snake case plus underscored: true, but that's all. I was ready to migrate a big RoR application to JS using express+sequelize.
Not only the old projects. I use snake case in my javascript codebase on purpose to distinguish ORM object's names from rest of the code. Then it is obvious that those data came from/go to database and it is in line with the reality, so I don't have to change the variable names for any raw query replacements. Current state doesn't break anything, it just bloats the query with unnecessary aliases so it is true pain to read it while debugging. Such a simple fix which doesn't brake anything and yet ignored.
P1: importantFor issues and PRs.docsFor issues and PRs. Things related to documentation, such as changes in the manuals / API reference.type: bugDEPRECATED: replace with the "bug" issue type
Activity
frastlin commentedon Jul 29, 2019
Well, the documentation is rather clear on how to use the underscored functionality, and if there is another way to use it, I would love to know.
I'm making a couple hooks today for my server to simulate the underscored functionality until this gets resolved.
I don't mind how the tables look in the database, when I use this, I want foreignId to be foreign_id when I interact with items in the database.
mb8z commentedon Aug 19, 2019
@frastlin Would you like to share those hooks? I would greatly appreciate it! 🎉
frastlin commentedon Aug 19, 2019
My solution is pretty unique to FeathersJS, the framework I'm using.
I have a before hook and an after hook. The before hook checks on a create method, if any of the keys in the objects end with "_id". If they do, they are added to a list. Then that list is checked if the key.slice(0, key.length-3) is in the list of hooks that I have manually created. If it is, then the hook is run through a camelize function.
The after hook also has a list of keys that may have the Id after them. So on find and get functions, I filter all the keys by the ones that end with "Id", then check if the key without "Id" is in the list of keys that I provided. Then I run an underscore function on the resulting keys.
It's not the best method and I really can't wait until this bug is fixed!
mb8z commentedon Aug 19, 2019
@frastlin Thanks a lot for the input. The bug is indeed frustrating. Cost me spending at least 2 hours of looking what I do wrong that it does not work.
matiasmenker commentedon Aug 19, 2019
Some update about the issues? Some one that solve this problem?
matiasmenker commentedon Aug 19, 2019
Can you please share?
papb commentedon Aug 19, 2019
Hello, just passing by to say that I did not forget about this. Finding time is not easy... Thanks everyone for the patience so far 👍
yoshiya0503 commentedon Sep 3, 2019
[My workaround plan until fixed]
[1] first of all, set global configuration as follows.
[2] define "foreign key" and "created_at, updated_at" to your model explicitly like as follows.
serge1peshcoff commentedon Sep 3, 2019
For those stumbled upon this issue, here's how I've fixed it:
16 remaining items
fix: _created_at/_updated_at bug
joshparolin commentedon Aug 11, 2020
Just a note that you still have to assign a "foreignKey" value and explicitly name your underscored value as of v6.3.3. Otherwise,
findAllwill return both your underscored version and a camel case version.sfratini commentedon Sep 29, 2020
In addition to what @joshparolin is saying, that is still not working on some cases. I have all my foreign keys specified as snake_case and when i try to run a setXXX with a many-to-many relationship it fails but only when I am trying to add new records to the join table. Not if I try to delete.
Eg. if the community has features [1, 2, 3] and I run setFeatures([1,2]) it works. But if the community has [1, 2, 3] and I run setFeatures([3, 4, 5]) it breaks.
But the error that I get is this:
As you can see, the instance has both camelCase and snake_case but in this case only one of them has the right values so the entire query is failing.
My goal is to use snake_case on the DB but camelCase on my GraphQL API.
My solution was to actually point my join table model foreignKeys to the camelCase that I defined on the model. While I can see why this works, these are some manual steps I don't think I should be doing when all the information for the fields is available:
jony89 commentedon Sep 19, 2021
since it's been over than a year with this issue, def should be added in the documentation as a known issue under the "Upgrade to V6/V5" section. @papb
github-actions commentedon Nov 8, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
TheOptimisticFactory commentedon Nov 8, 2021
Don't mind me, I am just un-staling this issue, as it is still an issue
yisibl commentedon Dec 28, 2022
Hi everyone, any progress here?
ephys commentedon Dec 28, 2022
That depends on what your issue is, since this issue regroups a bunch of them.
I'm actually going to close this issue as we have no idea which bug has been fixed and which bug remains.
If you're still experiencing an issue, please either comment in the relevant closed issue (listed in the first post of this thread) that you're still experiencing that issue and it should be re-opened, or open a new bug report.
yevhen309 commentedon Apr 20, 2023
What a surprise for people who need to update some old projects with the newer version of sequelize... What if you have zillions of models and _ is used everywhere in the codebase (and it's not typescript). Do you have to search and fix it manually? This option is required for backwards compatibility.
meruen commentedon Aug 1, 2024
This is very sad. 5 years now and still not fixed. I'm using
"sequelize": "^6.37.3"and"sequelize-cli": "^6.6.2"and my timestamps are still in camelCase. I used sequelize-cli passing the field names in snake case plusunderscored: true, but that's all. I was ready to migrate a big RoR application to JS using express+sequelize.mysuf commentedon Oct 27, 2024
Not only the old projects. I use snake case in my javascript codebase on purpose to distinguish ORM object's names from rest of the code. Then it is obvious that those data came from/go to database and it is in line with the reality, so I don't have to change the variable names for any raw query replacements. Current state doesn't break anything, it just bloats the query with unnecessary aliases so it is true pain to read it while debugging. Such a simple fix which doesn't brake anything and yet ignored.