miLibris / flask-rest-jsonapi

Flask extension to build REST APIs around JSONAPI 1.0 specification.
http://flask-rest-jsonapi.readthedocs.io
MIT License
597 stars 153 forks source link

Renaming columns in model #60

Closed kumy closed 7 years ago

kumy commented 7 years ago

I'm rewriting an old application where tables and columns names are in a foreign language. I would like to translate all to English as soon as possible in the code and limit use of non-english in the code. I also want to fix some inconsistencies in columns names.

First idea was to transform:

class User(db.Model):
    __tablename__ = 'users'
    userid = db.Column(db.Integer, primary_key=True)
    user = db.Column(db.String)

to

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column('userid', db.Integer, primary_key=True)
    name = db.Column(db.String)

but it raise: Exception: User has no attribute userid

Adding key='id' has no effect. (id = db.Column('userid', db.Integer, primary_key=True, key='id'))

I'll propose a PR for that.

firdaus-aziz commented 7 years ago

what about specifying attribute in schema?

http://marshmallow.readthedocs.io/en/latest/quickstart.html#specifying-attribute-names

kumy commented 7 years ago

This was my first idea, but it only permit to rename the field on Json dump level. All models and accesses will be burden by foreign languages strings. Renaming field as soon as possible (in model definition) will give a central point to update later when the old application will be dismantled, and databases fields renamed.