db icon indicating copy to clipboard operation
db copied to clipboard

Table names through reflection

Open dsincl12 opened this issue 3 years ago • 3 comments

Hi everyone,

This project really seem to fill a need I have and I'm mighty glad I stumbled upon it again :)

A quick question:

I just went through the tour and my first reaction when I saw col := sess.Collection("books") was, why isn't that handled with reflection? Any sizeable project will sooner or later end up with a typo.

It seems trivial to just use a more advanced version of:

a := model.Account{} t := reflect.TypeOf(a) t.Name() -> Account

But handling Pascal casing and pluralization (i.e. TaxRate -> tax_rates) etc to get the correct table names.

The result would be something like col := sess.Collection(model.Person) which would be a more type safe way to handle things.

Is there a reason why this hasn't been done or would a PR to add this be welcomed?

Keep up the good work!

dsincl12 avatar May 06 '21 21:05 dsincl12

I think it is inappropriate to use reflection to get the model name. I don’t think it’s a good choice to put the table prefix in the model name.

lenuse avatar May 18 '21 10:05 lenuse

Django does both things to incredible success. Do you have more concrete reasoning than "I don't like it"?

Asday avatar May 18 '21 11:05 Asday

You are right, a complete table name mapping should include prefix name configuration, structure name reflection, and custom table name.

lenuse avatar May 18 '21 12:05 lenuse