Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to create index with @Index Decorator when using table option of undescore:true #1714

Open
1 task
OkoyeApps opened this issue Mar 17, 2024 · 0 comments

Comments

@OkoyeApps
Copy link

OkoyeApps commented Mar 17, 2024

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: true option 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:true option 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant