Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlalchemy support class property, hope to support it #719

Open
oleeks opened this issue Aug 26, 2020 · 0 comments
Open

sqlalchemy support class property, hope to support it #719

oleeks opened this issue Aug 26, 2020 · 0 comments

Comments

@oleeks
Copy link

oleeks commented Aug 26, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants