Adonis make:model generating model with different name

Asked

Viewed 331 times

0

I executed the Adonis command

make:model Caredperson -mc

so that the controller and Migration were created together. After executing the command, I tried to change the name of the model, Migration and controller to Caredpeople, but when doing the belongsToMany('App/Models/Caredpeople') relationship of the Adonis referencing the model, the keys searched in the pivot table and the name of the pivot table are named "person" and not "people", I tried to pass the keys and table but did not work... After that, I tried to delete the model, controller and Migration and then run the creation command again with the correct name:

make:model Caredpeople -mc

but when running, the file created for all three comes with the word "person", for example model: Caredperson; controller: Caredpersoncontroller; Migration: cared_person; even if I executed the command by passing a different name.

I tried to download the project on another machine and execute the command, hoping to be some cache because it happens the same... Someone has an idea how to solve?

2 answers

1

There is no way to "solve" this, since it is not a mistake, but a convention.

Like other frameworks such as Ruby on Rails or Laravel, Adonisjs has a number of conventions regarding how your code should be done. Among them is a convention defining the rules of nomenclature for models and table names.

According to the Adonis convention, models must always be in the singular (and PascalCase), while tables should be plural (and snake_case).

That way, there is nothing wrong. English, Person is the singular of People, plural. So, when you run the command, Adonis will create a model in the singular (Person) and the tables (in Migrations) using the plural (people).

1

One can circumvent this naming convention used by Adonis by informing in the Model the name of the table to be used in Migration.

    class User extends Model {
  static get table () {
    return 'my_users'
  }
}

Reference Manual Adonis 4.1

  • In my view this answer will solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.