sequelize-typescript: createIndexDecorator has problems with "underscored" or "field" params
Versions
- sequelize: 5.21.3
- sequelize-typescript: 1.1.0
- typescript 3.7.4
I’m submitting a … [x] bug report [ ] feature request
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.
Expected behavior: Successful creation of index.
Steps to reproduce: Code is below.
Related code:
const OwnedItemUniqueIndex = createIndexDecorator({type: "UNIQUE"});
@Table({
tableName: "game_owned_items",
underscored: true
})
export default class OwnedItem extends Model<OwnedItem> {
@PrimaryKey
@AutoIncrement
@Column
public id: number;
@OwnedItemUniqueIndex
@Column
public ownerId: number;
@OwnedItemUniqueIndex
@Column
public itemId: number;
}
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 17 (3 by maintainers)
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:I’ve also been experimenting with a decorator wrapper:
Then replace
@Index
with@UnderscoredIndex
.Still not optimal.
Same here when i’m set underscored property is true (Cuz they missing underscore when create indexing) It is critical issues on me … ;
Same here. It’s about
@Index
itself, 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…
I’m still having this problem with sequalize-typescript - 2.1.5
If anyone is interested, extended @jessemyers 's solution to a generic decorator, so we could use it with parameters:
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 thecreateIndexDecorator
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.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_field
andmyInstance.myField
.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
Fixed as part of the merge of sequelize-typescript in sequelize https://github.com/sequelize/sequelize/pull/15482
I have a similar problem where:
raises:
My workaround is:
I would love not to have to do this…
“Temporary fix”
Works with PostgreSQL.