Skip to content

sqlalchemy support class property, hope to support it #719

@oleeks

Description

@oleeks

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 #685 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions