I tried to have a model field named differently than its DB table column, as it is possible for example in peewee. I found that you can specify the name of the column with its first argument, as it's described in SQLAlchemy docs:
Column('data', String(50))
So I tried to follow the similar notation in my models:
class DBUserGroup(db.Model):
__tablename__ = 'usergroup'
id = db.Column('ug_id', db.Integer(), primary_key=True)
name = db.Column('ug_name', db.String(length=255), nullable=False, unique=True)
I expected that the table columns will be named ug_id and ug_name while the model will use the fields id and name, like DBUserGroup.create(id=..., name=...) and usergroup.id, usergroup.name where _usergroup_ is an instance of _DBUserGroup_.
await db.gino.create_all(), it creates a table "usergroup" with fields id and name, instead of the expected ug_id and ug_name.DBUserGroup.create(id=1, name='guest'), it raises an exception: sqlalchemy.exc.CompileError: Unconsumed column names: id, nameApparently gino & sqlalchemy just don't expect me to name the model fields differently than the table columns.
But maybe I'm doing it wrong here and there is actually a way to do that?
No, there is no way yet - the property name is assumed to be identical to the column name.
It may take some time to add this support, but I think it is possible.
So what's the method to add custom column name?
@weaming it's just like SQLAlchemy, or the snippet in the issue raise. The property name of the model and the first argument of Column can be different.
any updates?
any updates?
the issue is fixed, you can now use custom column names, see the commit above
Most helpful comment
No, there is no way yet - the property name is assumed to be identical to the column name.
It may take some time to add this support, but I think it is possible.