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
underscore / camelcase option should control all fields and table name generation #6423
It would be great if sequelize will generate PostgreSQL DDL with lower_case_underscored table name and column names. By now underscore option manage foreign keys and create_at, modifed_at fields only.
In short
CREATE TABLE IF NOT EXISTS "Male" ("CamelCase" VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL, "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL);
should become
CREATE TABLE IF NOT EXISTS male (camel_case VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE NOT NULL, "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL);
Reacted by John D'Agostino, Olivier Lambeaux, Mad Max Summers, Aleksey Bobyr, Jerome Carty, Elvis Luciano Guimarães, Quoc Vu, Dzmitryi Hilimovich, hahahu, maxime.io and 40 more
changed the title [-]model underscore should produce underscored table_name and field_name's[/-][+]underscore option should produce underscored_table_name and underscored_field_name's[/+]on Aug 12, 2016
Correct, but there is lots of work involved with that 😆 , I would prefer one single option based control.
If anyone can share their experiences from other ORMs like ActiveRecord (Rails) or Eloquent (Laravel / PHP), that will be really helpful to make this decision.
Reacted by Raul Contreras, Weslei Prudencio, Alexis Hénaut, Abel Osorio, Nate, Abdelkader Boudih, Max Jacobson, Ihor Dotsenko, Tatiana McGarry, Dave Sag and 9 more
changed the title [-]underscore option should produce underscored_table_name and underscored_field_name's[/-][+]underscore / camelcase option should control all fields and table name generation[/+]on Aug 12, 2016
I also prefer underscored columns and tables and camelCase attributes and PascalCase model names. I think the ORM should allow to map this easily.
Reacted by Mad Max Summers, Benjamin E White, Jerome Carty, Elvis Luciano Guimarães, Jerod King, Shaomeng Zhang, Dzmitryi Hilimovich, Manthan Mallikarjun, Molecular Man, Douglas Mak and 63 more
It took me a long time to figure out why underscored option didn't convert my camelCase column names to under_scored ones.
I would also prefer to declare my columns as camelCased ones to be automatically converted later to underscored ones in SQL.
Reacted by Felix Becker, Benjamin E White, Jerome Carty, Leandro Glossman, Cayman, Elvis Luciano Guimarães, ian, Jonatan E. Salas, matt212, m.e. and 71 more
I also agree w/ @alexey2baranov. I know we use of the field option, but that makes the model more verbose. Typically, when we name a field fooBar, it is to keep it consistent w/ the rest of the javascript code and not that we really want fooBar as a column name.
The current behavior is not desirable. Who would want mixed column naming scheme in their tables?
At the minimum, the documentation should be fixed. It doesn't say underscored only applies to auto-generated fields.
Converts all camelCased columns to underscored if true. Will not affect timestamp fields named explicitly by model options and will not affect fields with explicitly set field option
Reacted by Alberto Naperi Jr., ian, m.e., user3587412, Jerod King, Dzmitryi Hilimovich, Mike Gallagher, Shlomi Levi, Jonathan Bender, Min and 15 more
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
I'm afraid that, although those cool hooks work when"force: true", they do not when using alter: true.
Since Sequelize checks the db schema usinc describeTable() it can't do the decamelize/camelize process without editing the package code.
Does anyone have a solution for this (apart from editing the Sequelize source code)?
I would like to propose / share how I envision implementation of this change. I would love to hear comments before this actually gets implemented.
There will be only one option underscored (underscoredAll and underscored will be merged)
Attributes
Setting Model.options.underscored: true will
set field to a name constructed by underscoring attribute. So attribute myColumn will have field my_column
If an attribute already got field defined, underscore will not override field for that attribute
Model.tableName
If Model.options.tableName is defined use it
if Model.options.freezeTableName: true set table name to model name
Otherwise pluralize Model.name then underscore it if underscored: true
Auto-generated Attributes (timestamps / version / foreignKeys)
OLD (Current)
If Model.options.underscored: true, then all attributes are generated with underscored name. like Model.rawAttributes.created_at / updated_at / foreign_key` etc
NEW (Proposed)
All attributes will be generated with camelcased name like Model.rawAttributes.createdAt / updateAt / foreignKey etc, but field will be set to underscored name like created_at / updated_at / foreign_key if Model.options.underscored: true
TLDR;
All attributes will be generated with camelcase naming by default, specifying underscore: true will set field by underscoring attribute name
Still using custom attribute name
Foreign keys, you will be able to pass association.options.foreignKey where applicable that attribute name will be used.
// define Task.user_id, use of `user_id` attribute as foreignKey on TaskUser.hasMany(Task,{foreignKey: 'user_id'});Task.belongsTo(User,{foreignKey: 'user_id'});
Timestamps
sequelize.define('Model',{name: Sequelize.STRING},{createdAt: 'created_at',// will define attribute created_at on ModelupdatedAt: 'updated_at'// will define attribute updated_at on Model})
Reacted by Kevin Yue, netsgnut, Dmitry Lukichev, Giancarlos Isasi, Jake Gavin, Yannick Drolet, !opips, Mariano Cavallo, Mars Wong, André Andrade and 3 more
Reacted by misos1, Kevin Carmody, Fabiano Amaral Freitas, Sergey Peshkov, Wei Feng, William Coates, Mikhail Bolshakov, bestvow, Antonis , Elliot Fleming and 9 more
I am surprised that underscored does not affect association and user still need to use { as: 'something_underscored' }, maybe this specification should mentioned it? It is not really underscored mode that what have been expected.
// ./db/fixedDb.mjsimportSequelizefrom'sequelize'importconfigfrom'./config.mjs'importfoofrom'./foo.mjs'constfixedConfig={
...config,define: {
...config.define,// You HAVE TO add those...createdAt: 'created_at',updatedAt: 'updated_at',deletedAt: 'deleted_at'}}constsequelize=newSequelize(fixedConfig)constDataTypes=Sequelize.DataTypesletdb={
sequelize,foo: foo(sequelize,DataTypes)}exportdefaultdb
createdAt : 'created_at', // will define attribute created_at on Model
updatedAt : 'updated_at' , // will define attribute updated_at on Model
deletedAt: 'deleted_at' ,
, {
createdAt : 'created_at', // will define attribute created_at on Model
updatedAt : 'updated_at' , // will define attribute updated_at on Model
deletedAt: 'deleted_at' ,
Activity
[-]model underscore should produce underscored table_name and field_name's[/-][+]underscore option should produce underscored_table_name and underscored_field_name's[/+]sushantdhiman commentedon Aug 12, 2016
Sounds like a reasonable feature to me, @janmeier ?
janmeier commentedon Aug 12, 2016
I don't think this is needed - If users want underscored names, they can simply define them as such, or use the
fieldoption.The
underscoredoption is for controlling auto-generated column names, but when a user tells us about the columnfooBar, we call it fooBar ;)This can easily be solved with a
beforeDefinehooksushantdhiman commentedon Aug 12, 2016
Correct, but there is lots of work involved with that 😆 , I would prefer one single option based control.
If anyone can share their experiences from other ORMs like
ActiveRecord(Rails) orEloquent(Laravel / PHP), that will be really helpful to make this decision.[-]underscore option should produce underscored_table_name and underscored_field_name's[/-][+]underscore / camelcase option should control all fields and table name generation[/+]felixfbecker commentedon Aug 12, 2016
I also prefer underscored columns and tables and camelCase attributes and PascalCase model names. I think the ORM should allow to map this easily.
maxsummers commentedon Jan 3, 2017
It took me a long time to figure out why
underscoredoption didn't convert mycamelCasecolumn names tounder_scoredones.I would also prefer to declare my columns as
camelCasedones to be automatically converted later to underscored ones in SQL.elvisgs commentedon May 5, 2017
+1
quocvu commentedon Jun 1, 2017
I also agree w/ @alexey2baranov. I know we use of the
fieldoption, but that makes the model more verbose. Typically, when we name a fieldfooBar, it is to keep it consistent w/ the rest of the javascript code and not that we really wantfooBaras a column name.The current behavior is not desirable. Who would want mixed column naming scheme in their tables?
At the minimum, the documentation should be fixed. It doesn't say
underscoredonly applies to auto-generated fields.pgollangi commentedon Jun 5, 2017
+1
stale commentedon Aug 12, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
alexey2baranov commentedon Aug 12, 2017
Up
38 remaining items
fix(model): docs changed for sequelize#6423 - PR sequelize#9260
fix(Model): Consistent behaviour for system timestamp properties sequ…
knoxcard commentedon Apr 4, 2018
@rogerc - I currently use MariaDB, but good to know if I ever switch to PostgreSQL, one less thing to worry about!
didac-wh commentedon Apr 4, 2018
I'm afraid that, although those cool hooks work when"force: true", they do not when using alter: true.
Since Sequelize checks the db schema usinc describeTable() it can't do the decamelize/camelize process without editing the package code.
Does anyone have a solution for this (apart from editing the Sequelize source code)?
sushantdhiman commentedon Apr 7, 2018
I would like to propose / share how I envision implementation of this change. I would love to hear comments before this actually gets implemented.
There will be only one option
underscored(underscoredAllandunderscoredwill be merged)Attributes
Setting
Model.options.underscored: truewillfieldto a name constructed by underscoringattribute. So attributemyColumnwill have fieldmy_columnfielddefined,underscorewill not overridefieldfor that attributeModel.tableName
Model.options.tableNameis defined use itModel.options.freezeTableName: trueset table name to model nameModel.namethen underscore it ifunderscored: trueAuto-generated Attributes (timestamps / version / foreignKeys)
OLD (Current)
If
Model.options.underscored: true, then all attributes are generated with underscored name. like Model.rawAttributes.created_at / updated_at / foreign_key` etcNEW (Proposed)
All attributes will be generated with camelcased name like
Model.rawAttributes.createdAt / updateAt / foreignKeyetc, but field will be set to underscored name likecreated_at/updated_at/foreign_keyifModel.options.underscored: trueTLDR;
All attributes will be generated with camelcase naming by default, specifying
underscore: truewill setfieldby underscoring attribute nameStill using custom attribute name
association.options.foreignKeywhere applicable that attribute name will be used.rogerc commentedon Apr 7, 2018
Sounds good. My PR I think covered all attributes except foreignKeys. Table name was left untouched.
pickfire commentedon Mar 13, 2019
I am surprised that
underscoreddoes not affect association and user still need to use{ as: 'something_underscored' }, maybe this specification should mentioned it? It is not really underscored mode that what have been expected.up9cloud commentedon May 27, 2019
Memo for whom upgrade v4 to v5 with
underscored: trueand get unexpected field names (createdAt, updatedAt, deletedAt) from query result:TL; DR
In v4 you can just set
underscored: trueand ignore thoseIn v5, you HAVE TO add them to your define options, otherwise you'll get camel cased field names in your query result 😭 .
Details
v4 original
v5
v5 fixed
Test
leodutra commentedon May 28, 2019
The underscore is not common in JavaScript properties, so makes more sense to consider camel case first.
papb commentedon Aug 29, 2019
Related: #11225
CarlosDanielDev commentedon Jan 28, 2020
How can I change
underscoredAllto Camel Case ?codeyourwayup commentedon Jan 25, 2022
createdAt : 'created_at', // will define attribute created_at on Model
updatedAt : 'updated_at' , // will define attribute updated_at on Model
deletedAt: 'deleted_at' ,
codeyourwayup commentedon Jan 25, 2022
this is how to fix
{
, {
createdAt : 'created_at', // will define attribute created_at on Model
updatedAt : 'updated_at' , // will define attribute updated_at on Model
deletedAt: 'deleted_at' ,
}}
weijie0 commentedon Mar 31, 2022
// 你可以通过 'field' 属性指定自定义列名称:
fieldWithUnderscores: { type: DataTypes.STRING, field: 'field_with_underscores' },