Penaf's avatar
Level 1

Model names for weird table names

Having inherited a pretty complex mysql database I'm faced with table names that I really don't know how should I name the models for them.

I tried to read naming conventions like this one https://github.com/alexeymezenin/laravel-best-practices#follow-laravel-naming-conventions without reaching any conclusion how should I best name the model.

I have about 30 tables with names like this one:

Table name: JB_CogHospedeiros Field: idCogHospedeiros (Primary Key) Field: descCogHop (varchar 300)

How should I name the model for this table?

Bonus question: Should I make a migration and seed also for future deployments?

Thanks in advance for all the help possible.

0 likes
4 replies
Cronix's avatar

That db design and naming convention would drive me nuts.

Personally, I'd recreate the database using sane table/field names and follow laravel conventions (like 'id' instead of idCogHospedeiros for the pk name, etc).

I'd create migrations to recreate the sanely named tables/fields, using the plural of the word it represents (users vs user) and create the models using the singular form (User vs Users).

I'd create a seeder that would take the old data, and insert it into the new db.

I know that's not a small/trivial thing to do, but once done it will make the app a lot easier to code and fit within laravel without a lot of additional modifications (like having to override the table name in the model, or the primaryKey, or having to manually enter id's in relationship definitions, etc).

I've inherited monstrosities like that in the past and tried to make do, only to waste lots of time (and frustration) making things work with the original crap structure. Once I converted it all to laravel conventions, it was a breeze to convert the rest.

Penaf's avatar
Level 1

Yeah ... it's driving me nuts @Cronix !

What about for cases like this one:

TABLE: Especie (PK: idEspecie) TABLE: SpecificGroup (PK: idSpecificGroup) TABLE: EspecieSpecificGroup (2 Fields: Especie_idEspeice & SpecificGroup_idSpecificGroup)

I'd rename idEspecie to id ... on the other table idSpecificGroup to id ... what would I do on the table EspecieSpecificGroup? How would I rename those 2 fields since this table only relates entries from the other table?

Cronix's avatar
Cronix
Best Answer
Level 67

looks like a lookup table, so using laravel conventions it would be tablename_id for each of the fields.

species (model: Specie - might have to be Species since plural/singular is the same thing)
-id

specific_groups (model: SpecificGroup)
-id

especie_specific_group (no model - lookup table)
-specie_id
-specific_group_id

I'm not sure if those "words" are correct for pluralization. I'm an English speaker so I'm used to English names.

1 like
jlrdw's avatar

Table name: JB_CogHospedeiros

Name model Fred

Please or to participate in this conversation.