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

sqlalchemy2.x类的属性反序列化会报错 #1

Open
rufeng-h opened this issue Apr 14, 2023 · 1 comment
Open

sqlalchemy2.x类的属性反序列化会报错 #1

rufeng-h opened this issue Apr 14, 2023 · 1 comment

Comments

@rufeng-h
Copy link

rufeng-h commented Apr 14, 2023

序列化反序列化很好用的一个库,不过本人最近在用sqlalchemy2.x,类的属性反序列化会报错

from datetime import datetime

from objtyping import objtyping
from sqlalchemy import Integer, String, VARCHAR, DateTime
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped, mapped_column


class Base(DeclarativeBase):
    pass


class Area(Base):
    __tablename__ = 'area'
    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, nullable=False)
    code: Mapped[str] = mapped_column(String(12), nullable=False)
    name: Mapped[str] = mapped_column(VARCHAR(15), nullable=False)
    level: Mapped[int] = mapped_column(Integer, nullable=False)
    pcode: Mapped[str] = mapped_column(String(12), nullable=False)
    create_time: Mapped[datetime] = mapped_column(DateTime, nullable=False)
    update_time: Mapped[datetime] = mapped_column(DateTime, onupdate=datetime.now)

    def __str__(self):
        return self.name

    __repr__ = __str__


if __name__ == '__main__':
    area = Area(id=1, code='213123131', name='里斯', level=1, pcode='1212313131')
    s = objtyping.to_primitive(area)
    print(s)
    print(objtyping.from_primitive(s, Area))

会将数据库字段判断为sqlalchemy.orm.MappedColumn类型,但实际值为基本类型

@songofhawk
Copy link
Owner

songofhawk commented Apr 17, 2023 via email

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

No branches or pull requests

2 participants