Last active
December 2, 2022 18:07
-
-
Save mydreambei-ai/ef31f183396b009df81faeb6b5940e49 to your computer and use it in GitHub Desktop.
sqlalchemy postgresql json data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Column, String, ForeignKey | |
from sqlalchemy.orm import relationship | |
from sqlalchemy.ext.declarative import declarative_base | |
Base = declarative_base() | |
class City(Base): | |
__tablename__ = "citys" | |
city = Column(String(20), unique=True) | |
city_id = Column(String(6), primary_key=True) | |
father_id = Column(String(6)) | |
areas = relationship('Area', cascade='delete', backref="city") | |
class Area(Base): | |
__tablename__ = "areas" | |
area = Column(String(50), unique=True) | |
area_id = Column(String(6), primary_key=True) | |
father_id = Column(ForeignKey('citys.city_id')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COPY citys (city, city_id, father_id) FROM stdin; | |
郑州市 410100 410000 | |
开封市 410200 410000 | |
\. | |
COPY areas (area, area_id, father_id) FROM stdin; | |
郑州市 410101 410100 | |
二七区 410102 410100 | |
中原区 410103 410100 | |
惠济区 410104 410100 | |
管城区 410105 410100 | |
金水区 410106 410100 | |
上街区 410107 410100 | |
郑东新区 410108 410100 | |
航空港区 410109 410100 | |
综合保税区 410110 410100 | |
高新技术开发区 410111 410100 | |
国家经济技术开发区 410112 410100 | |
荥阳市 410113 410100 | |
登封市 410114 410100 | |
新密市 410115 410100 | |
新郑市 410116 410100 | |
中牟县 410117 410100 | |
开封市 410201 410200 | |
鼓楼区 410202 410200 | |
祥符区 410203 410200 | |
龙亭区 410204 410200 | |
金明区 410205 410200 | |
禹王台区 410206 410200 | |
开封新区 410207 410200 | |
顺河回族区 410208 410200 | |
杞县 410209 410200 | |
通许县 410210 410200 | |
尉氏县 410211 410200 | |
\. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.dialects import postgresql | |
from sqlalchemy.sql import column | |
from sqlalchemy.sql.functions import func | |
from sqlalchemy import create_engine | |
from models import City, Area | |
session = create_engine("") | |
from sqlalchemy import select | |
# using text method | |
stmt = select([City.city, Area.area, City.city_id, Area.area_id]).where(City.city_id == Area.father_id).alias('t') | |
t = select([text("json_agg(json_build_object('city_id', city_id, 'area_id', area_id)), city")]).select_from(stmt).group_by(stmt.c.city) | |
r = session.execute(t).scalar() | |
print(r) | |
from sqlalchemy.sql.functions import GenericFunction | |
class json_agg(GenericFunction): | |
type = postgresql.JSON | |
class json_object_agg(GenericFunction): | |
type = postgresql.JSON | |
class json_build_object(GenericFunction): | |
type = postgresql.JSON | |
t = select([City.city, City.city_id]).alias("ttt") | |
t = select([func.row_to_json(column('ttt'))]).select_from(t) | |
print(t) | |
# using GenericFunction | |
t = select([func.json_agg(func.json_build_object(stmt.c.area, stmt.c.area_id)), stmt.c.city]).select_from(stmt).group_by(stmt.c.city) | |
print(t) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment