Gino: Any way to have custom column names?

Created on 26 Jun 2018  路  5Comments  路  Source: python-gino/gino

  • GINO version: 0.7.4
  • Python version: 3.6.5
  • asyncpg version: 0.16.0
  • aiocontextvars version: 0.1.2
  • PostgreSQL version: 10.4

Description

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))

What I Did

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)

What I Expected

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_.

What Actually Happens

  1. When I do await db.gino.create_all(), it creates a table "usergroup" with fields id and name, instead of the expected ug_id and ug_name.
  2. When I try to do DBUserGroup.create(id=1, name='guest'), it raises an exception: sqlalchemy.exc.CompileError: Unconsumed column names: id, name

Apparently 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?

feature request

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.

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmillssf picture pmillssf  路  3Comments

phoolish-philomath picture phoolish-philomath  路  3Comments

myraygunbarrel picture myraygunbarrel  路  3Comments

AmatanHead picture AmatanHead  路  5Comments

marcojulioarg picture marcojulioarg  路  3Comments