gino icon indicating copy to clipboard operation
gino copied to clipboard

sqlalchemy support class property, hope to support it

Open oleeks opened this issue 4 years ago • 0 comments

create @oleeks it's not a bug. You'll need to use _password instead of password since the property of the class is _password, and password is the column name, so it should not be used to create the instance.

I get what you're trying to do, but this doesn't work as property on Model.create.

@wwwjfy thanks, but sqlalchemy can be used like this, so I thought it was compatible

DB_URI = 'mysql+pymysql://{username}:{password}@{hort}:{post}/{db}?charset-utf8'.format(
    username=USERNAME, password=PASSWORD, hort=HOST, post=POST, db=DATABASE)

engine = create_engine(DB_URI)
Base = declarative_base(engine)

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(20))
    age = Column(Integer)
    _password = Column(String(128), name='password', nullable=False)
    
    @property
    def password(self):
        raise AttributeError('password is not readable')

    @password.setter
    def password(self, password):
        self._password = password


# Base.metadata.create_all()

session = sessionmaker(engine)()

def init_data():
    user = User(name='fantix', age=18, password='123456')
    session.add(user)
    session.commit()

if __name__ == "__main__":
    init_data()

Originally posted by @oleeks in https://github.com/python-gino/gino/pull/685#issuecomment-680433883

oleeks avatar Aug 26 '20 03:08 oleeks