sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.78k stars 279 forks source link

I neet remove attribute id in model #1694

Open AnechaS opened 1 year ago

AnechaS commented 1 year ago

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.
blankstar85 commented 11 months ago

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

AnechaS commented 11 months ago

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

blankstar85 commented 11 months ago

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; }`

cth166 commented 11 months ago

give a @PrimaryKey in other filed