sequelize-typescript
sequelize-typescript copied to clipboard
Failed to create index with @Index Decorator when using table option of undescore:true
Issue
Versions
- sequelize: 6.37.1 (I also downgraded to 6.35.0)
- sequelize-typescript: 2.1.6
- typescript: ^5.1.3
Issue type
- [ x] bug report
- [ ] feature request
Actual behavior
When you set the underscore: true option in the @Table configuration and also use @Index Decorator, Sequelize-Typescript doesn't remember/consider this setting when creating the Index.
e.g If you have a column name of emailAddress and the underscore:true option is set in the configuration. Sequelize-Typescript goes ahead to try creating a column with the name emailAddress instead of email_address which results in an error.
Expected behavior
The configuration option should be remembered and used when creating an index with the Index Decorator.
Steps to reproduce
- Create a table
- Add the
underscore: trueoption to the table options - add a column that uses camelCase
- add an index to that column e.g
emailAddress - sequelize-typescript fails to create this index because it doesn't remember the
underscore:trueoption initially set
Related code
import { Optional } from "sequelize";
import { Table, Column, DataType, Index, PrimaryKey, Model } from "sequelize-typescript";
interface UserAttributes {
id: number;
phoneNumber: string;
password: string;
emailAddress: string;
userType: number;
isActive: boolean;
};
interface UserCreationAttributes extends Optional<UserAttributes, 'id' | 'emailAddress' | 'phoneNumber'> { };
@Table({
timestamps: true,
tableName: 'user',
underscored: true,
/**
* Using this syntax works as when debugging I noticed the issue and decided to use this syntax.
* I guess this behaviour is ok since you're opting in to specifying the field yourself
**/
// indexes: [
// { fields: ['email_address'], unique: true },
// { fields: ['phone_number'], unique: true, },
// ]
})
export class UserModel extends Model<UserAttributes, UserCreationAttributes>{
@PrimaryKey
@Column
id: number;
@Index
@Column({ allowNull: true, unique: true, type: DataType.STRING(255) })
emailAddress: string;
@Index
@Column({ allowNull: true, unique: true, type: DataType.STRING(32) })
phoneNumber: string;
@Column({ allowNull: false, type: DataType.SMALLINT })
userType: number;
@Column({ defaultValue: true, type: DataType.BOOLEAN })
isActive: boolean;
@Column({ allowNull: false, type: DataType.STRING(320) })
password: string;
};
@RobinBuschmann @Toxicable @lukashroch
I think it is so late but I hope It will be useful with someone who has this problem in the future.
@Table({ tableName: 'message', underscored: true })
export class MessageEntity extends Model {
@Index({ name: 'message_chat_id_index', unique: false })
chat_id: number;
@Column({ allowNull: false, type: BIGINT })
chatId: number;
}