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

I neet remove attribute id in model #1694

Open
AnechaS opened this issue Sep 8, 2023 · 4 comments
Open

I neet remove attribute id in model #1694

AnechaS opened this issue Sep 8, 2023 · 4 comments

Comments

@AnechaS
Copy link

AnechaS commented Sep 8, 2023

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
Copy link
Contributor

blankstar85 commented Sep 26, 2023

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
Copy link
Author

AnechaS commented Sep 26, 2023

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

@blankstar85
Copy link
Contributor

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
Copy link

cth166 commented Oct 5, 2023

give a @PrimaryKey in other filed

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

3 participants