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

Model generated - Declaring public class fields error - Typescript #600

Open
TheCelebrimbor opened this issue Feb 13, 2022 · 5 comments
Open

Comments

@TheCelebrimbor
Copy link

TheCelebrimbor commented Feb 13, 2022

I'm generating models with these switches on --cp p --cf p --cm p.

After the models are initialized, while querying, sequelize produces the following error.
(sequelize) Warning: Model "UsersToRoles" is declaring public class fields for attribute(s): "IdUser", "IdRole", "RoleName"

The model file has the following data generated..

export interface UsersToRolesAttributes {
  IdUser: number;
  IdRole: number;
  RoleName?: string;
}


export class UsersToRoles extends Model<UsersToRolesAttributes, UsersToRolesCreationAttributes> implements UsersToRolesAttributes {

  IdUser!: number;
  IdRole!: number;
  RoleName?: string;
.
.
.
.
Bunch of other stuff

static initModel (sequelize: Sequelize.Sequelize): typeof UsersToRoles {
  	return UsersToRoles.init({
  		IdUser: {
  			type: DataTypes.INTEGER,
  			allowNull: false,
  			primaryKey: true,
  			references: {
  				model: "user",
  				key: "id_user"
  			},
  			field: "id_user"
  		},
 .
.
.
.
}

As given here, If I add declare in front of the properties. The error goes away.

export class UsersToRoles extends Model<UsersToRolesAttributes, UsersToRolesCreationAttributes> implements UsersToRolesAttributes {

 declare IdUser: number;
  declare IdRole: number;
  declare RoleName?: string;

Using sequelize@6.15.1 and sequelize-auto@0.8.8
This issue did not occur in older versions with the same schema.... I upgraded sequelize and sequelize auto and this issue showed up.

Any help would be appreciated.

@TheCelebrimbor
Copy link
Author

Also, all the HasOne, HasMany getter/setter functions are undefined !!

@TheCelebrimbor
Copy link
Author

I am currently manually using declare keyword everywhere. It becomes a pain because there are sooo many tables and having to manually do changes to all the files defeats the purpose of using sequelize-auto in the first place.

@TheCelebrimbor
Copy link
Author

According to https://sequelize.org/master/manual/typescript.html. We do need to use declare everywhere. Hmm.

@steveschmitt
Copy link
Collaborator

Yeah, the later versions of typescript are making things harder.

@Tinymemo
Copy link

Tinymemo commented Nov 3, 2022

Any update on this? How can I solve this problem?

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