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
createIndexDecorator has problems with "underscored" or "field" params #725
Actual behavior:
I've tried using new feature - createIndexDecorator, but it doesn't take into account the underscored table param (field param on the column doesn't work either) - therefor it errors out on database level, because there are no such fields existing.
Reacted by Erik Mårtensson, Hwanseok Kang (Barth), Carlos Palacín, hax0r, Taewoong Jang, Nir Alfasi, Saulo Tauil, m.e., andreyvolokitin, klausXR and 1 more
EDIT: Don't use this over-complicated solution. Just use jessemyers solution here instead if you want a decorator that works.
Or use my other solution if you don't like decorators for some reason.
I also have this issue, and as jessemyers have pointed out, the problem is directly related to the @Index decorator, not just the createIndexDecorator function.
My solution was to use two fields, one with the correct name for the database (database field) and one virtual field for the code (application field) with magic get/set functions.
my_field = database field
myField = application field
This solution requires some extra code, but the application field will be fully working, just like a normal field. And the database field is persisted and named correctly in the database so when this bug is fixed you just need to remove this workaround code.
Just remember that, with this solution, when creating a new instance of this model, both the database and application fields will be available. Like myInstance.my_field and myInstance.myField.
I've found another potential fix for this, by not using the @Index decorator at all but instead just specifying the indices in the @Table decorator instead, and manually specifying the field(s). Like this:
@Table({tableName: 'user',underscored: true,indexes: [{// Column name with underscores herefields: ['first_name'],},],})exportclassUserextendsModel<User>{
@PrimaryKey
@Columnid: string;
@ColumnfirstName: string;});
Reacted by Andrei Solovev, Leandro Hoffmann, Shangbin Yang, Roman, zanminkian, Jesús Andrés, SeungBo and Lekan Oladee
Reacted by Jesse Myers, Andrei Solovev, khoatd152, Roman and Jesús Andrés
For those of us using @ranhalprin 's solution and using a subset of IndexFieldOptions in the decorator...
export function UnderscoredIndex(indexOptions: IndexOptions): AnnotationFunction;
export function UnderscoredIndex(indexOptions: IndexDecoratorOptions): AnnotationFunction;
This issue will not be fixed in sequelize-typescript, but it is fixed in @sequelize/core v7 which has TypeScript as a first class citizen and removes the need for sequelize-typescript. @sequelize/core v7 is still in alpha, but quite stable. Once the first full version is out we will start to deprecate sequelize-typescript
Activity
piotrmoszkowicz commentedon Jan 5, 2020
"Temporary fix"
Works with PostgreSQL.
RobinBuschmann commentedon Jan 8, 2020
Hey @piotrmoszkowicz, thanks for reporting.
jessemyers commentedon May 15, 2020
I have a similar problem where:
raises:
My workaround is:
I would love not to have to do this...
ErikMartensson commentedon May 15, 2020
EDIT: Don't use this over-complicated solution. Just use jessemyers solution here instead if you want a decorator that works.
Or use my other solution if you don't like decorators for some reason.
I also have this issue, and as jessemyers have pointed out, the problem is directly related to the@Indexdecorator, not just thecreateIndexDecoratorfunction.My solution was to use two fields, one with the correct name for the database (database field) and one virtual field for the code (application field) with magic get/set functions.This solution requires some extra code, but the application field will be fully working, just like a normal field. And the database field is persisted and named correctly in the database so when this bug is fixed you just need to remove this workaround code.Just remember that, with this solution, when creating a new instance of this model, both the database and application fields will be available. LikemyInstance.my_fieldandmyInstance.myField.rlaace423 commentedon May 25, 2020
Same here. It's about
@Indexitself, not just others.I think it is quite critical issue for who has already existing schemas.
It seems to me considering to change sequelize-typescript to TypeORM..
webhacking commentedon Jul 29, 2020
Same here when i'm set underscored property is true (Cuz they missing underscore when create indexing)
It is critical issues on me .. ;
ErikMartensson commentedon Sep 3, 2020
I've found another potential fix for this, by not using the
@Indexdecorator at all but instead just specifying the indices in the@Tabledecorator instead, and manually specifying the field(s). Like this:jessemyers commentedon Sep 3, 2020
I've also been experimenting with a decorator wrapper:
Then replace
@Indexwith@UnderscoredIndex.Still not optimal.
ranhalprin commentedon Feb 5, 2021
If anyone is interested, extended @jessemyers 's solution to a generic decorator, so we could use it with parameters:
klausXR commentedon Jun 23, 2022
Bump
Coinhexa commentedon Oct 14, 2022
Same issue duplicated at #1424
ephys commentedon Dec 19, 2022
Fixed as part of the merge of sequelize-typescript in sequelize sequelize/sequelize#15482
shota-zalando commentedon Sep 22, 2023
I'm still having this problem with sequalize-typescript - 2.1.5
argtoastar commentedon Dec 14, 2023
For those of us using @ranhalprin 's solution and using a subset of IndexFieldOptions in the decorator...
export function UnderscoredIndex(indexOptions: IndexOptions): AnnotationFunction;export function UnderscoredIndex(indexOptions: IndexDecoratorOptions): AnnotationFunction;
adilcpm commentedon Jan 6, 2024
still having problem with this on 2.1.6
WikiRik commentedon Jan 6, 2024
This issue will not be fixed in sequelize-typescript, but it is fixed in @sequelize/core v7 which has TypeScript as a first class citizen and removes the need for sequelize-typescript. @sequelize/core v7 is still in alpha, but quite stable. Once the first full version is out we will start to deprecate sequelize-typescript
adilcpm commentedon Jan 6, 2024
very nice. looking forward to it. Any ETA?