Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

mapping between orm_fieldname and interface_fieldname #154

Closed Jaymon closed 1 year ago

Jaymon commented 1 year ago

Recently I needed to mess with a table with column names in camel case (eg, fooBar) but I wanted to keep the model fields in snake case (eg, foo_bar) so I looked into passing a param into field, something like this:

foo_bar = Field(str, interface_name="fooBar")

and then just having everything magically work. I think this is possible, but it will take some refactoring. I think I could hook into Interface._normalize_name by having it take a Field instance or a Schema instance. But I would need to change up the interface to always have the Field instance close, then Field.interface_name could be called in that method to return the right name for the db.

Jaymon commented 1 year ago

Honestly, Field's aliasing funcitonality basically did this, I just had to reverse it:

fooBar = Field(str, alias="foo_bar")

Worked exactly how I wanted, the fooBar was in the db and I could use foo_bar everywhere in the codebase