Issue when adding "s" at the end of HasMany association field name
CaptainZiboo opened this issue · comments
Issue
I found that, when using ownedOrganization or organization, it worked, but, when using ownedOrganizations for the field name, or organizations, it didn't work ! Is it normal that we cannot add an "s" add the end of the field names ? Is it because of sequelize creating files such as getUserTests ? I hope I didn't do anything wrong, if I did just tell me ! Hope anyone can help ! 👌
Versions
- sequelize: ^6.29.1
- sequelize-typescript: ^2.1.5
- typescript: ^4.9.4
Issue type
- [ x] bug report
- feature request
Actual behavior
When trying to use the instance.$create method on a model, I receive an error
See models and code snippets for model creation and instance creations below
Expected behavior
Should be creating a new organization associated to the user
Steps to reproduce
Create a model with one to many relation with HasMany field name including ( associated model name with ) "s" at the end
Exemple : Organization -> Organizations
PS : Removing the "s" at the end of "Organizations" in the user model ( see code below ) resolve the issue
Related code
Error I get in the console
C:\Mes Fichiers\development\server\node_modules\sequelize-typescript\dist\model\model\model.js:51
return this['create' + (0, string_1.capitalize)(propertyKey)](values, options);
^
TypeError: this[("create" + (0 , string_1.capitalize)(...))] is not a function
at UserTest.$create (C:\Mes Fichiers\development\server\node_modules\sequelize-typescript\dist\model\model\model.js:51:70)
at C:\Mes Fichiers\development\server\src\loaders\sequelize.ts:47:37
at Generator.next (<anonymous>)
at fulfilled (C:\Mes Fichiers\development\server\src\loaders\sequelize.ts:5:58)
I'm using these two models
import { Column, HasMany, Model, Table } from "sequelize-typescript";
import { OrganizationTest } from "./organizationtest.model";
@Table
export class UserTest extends Model {
@Column
name!: string
@HasMany(() => OrganizationTest, 'owner')
ownedOrganizations!: OrganizationTest[];
}
import { BelongsTo, Column, Model, Table } from "sequelize-typescript";
import { UserTest } from "./usertest.model";
@Table
export class OrganizationTest extends Model {
@Column
name!: string
@BelongsTo(() => UserTest, 'owner')
_owner!: UserTest
}
And here is the code to create the user and associated organization
// Adding default admin user for testing purposes
const user = await UserTest.create({
username: 'admin'
})
Logger.info('Default admin user created !')
const organization = await user.$create('ownedOrganizations', {
name: 'My organization'
})
tsconfig
/* Language and Environment */
"target": "es6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
sequelize config
// Creating the main database instance
const database = new Sequelize('app', '', '', {
storage: 'database.sqlite',
dialect: 'sqlite',
logging: false,
models: [__dirname + '/../models/*.model.ts'],
modelMatch: (filename, member) => {
console.log(filename, member)
return filename.substring(0, filename.indexOf('.model')) === member.toLowerCase();
},
});
await database.sync({ force: true });