sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

Model generated - Declaring public class fields error - Typescript

Open TheCelebrimbor opened this issue 3 years ago • 5 comments

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 [email protected] and [email protected] 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 avatar Feb 13 '22 13:02 TheCelebrimbor

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

TheCelebrimbor avatar Feb 13 '22 14:02 TheCelebrimbor

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 avatar Feb 14 '22 05:02 TheCelebrimbor

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

TheCelebrimbor avatar Feb 14 '22 05:02 TheCelebrimbor

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

steveschmitt avatar Apr 28 '22 20:04 steveschmitt

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

Tinymemo avatar Nov 03 '22 04:11 Tinymemo