sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I neet remove attribute id in model

AnechaS opened this issue · comments

Nodejs v18.17.1
sequelize v 6.33.0
sequelize-typescript v2.1.5

@Table({
  tableName: 'USERS',
  timestamps: false,
})
export class User extends Model {
  @Column({ field: 'NAME' })
  name: string;

  @Column({ field: 'EMAIL' })
  email: string;
}

User.removeAttribute('id');

Error

throw new model_not_initialized_error_1.ModelNotInitializedError(this, `Member "${key}" cannot be called.`);
                      ^
ModelNotInitializedError: Model not initialized: Member "removeAttribute" cannot be called. "User" needs to be added to a Sequelize instance.
commented

You need to make one of the fields a primary key otherwise id is auto added as primary key

Also the error you are receiving is due to the model not being attached to the Sequelize instance, you can do that in one of two ways following the docs: https://github.com/sequelize/sequelize-typescript#configuration

Do you have an alternative solution for fixing the issue in the model file?

commented

As given you need to make a primary key for the model otherwise a id primarykey gets added automatically. If you don't want id as your primary key then mark another field as primary key as below, you can use email ( which should be unique) and gives you a string primary key.

`@Table({
tableName: 'USERS',
timestamps: false,
})
export class User extends Model {
@column({ field: 'NAME' })
name: string;

@PrimaryKey
@column({ field: 'EMAIL' })
email: string;
}`

commented

give a @PrimaryKey in other filed