python - 如何通过指定模式从 postgresql 数据库中获取行?

标签 python postgresql sqlalchemy

我是 Postgresql 和 Sqlalchemy 的新手。我有以下文件 layout.py。在此,我在“col”模式下创建了两个表名“layout”和“layout default”。

import json, decimal
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer, TIMESTAMP, Sequence, text, types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime, uuid


db_string = "postgres://postgres:PWDd@10.**.**.***:1111/d_demo"

Base = declarative_base()
db = create_engine(db_string)

class Layout(Base):
    __tablename__ = "col.layout"
    layout_id = Column(UUID(as_uuid=True), nullable=False, primary_key=True)
    name = Column(String(1000), nullable=False)
    layout = Column(String(10000), nullable=False)
    grid_id = Column(Integer, nullable=False)
    user_id = Column(Integer, nullable=False)
    issystemlayout = Column(Integer, default=0, nullable=False)
    ispublic = Column(Integer, default=0, nullable=False)
    isactive = Column(Integer, default=0, nullable=False)
    createdby = Column(Integer, default=1, nullable=False)
    createdat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
    modifiedat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
    modifiedby = Column(Integer, default=1, nullable=False)

插入数据:

INSERT INTO col.layout(layout_id,name,layout,grid_id,user_id,ispublic,issystemlayout,isactive,createdby,createdat, modifiedat,modifiedby) VALUES('ba0233d7-d917-4303-b4bf-c2544a617d33','Layout1','{"Name":"Manish","Place":"Pune"}',1,12345,'1','0','1','201819','2015/05/20','2015/05/16',123);

获取数据:

Session = sessionmaker(bind=db)  
session = Session()
Base.metadata.create_all(db)
session.query("SET search_path TO col;") 

result = []
selected_columns = Layout.__table__.columns
print("Selected columns {}".format(selected_columns))
record = session.query(Layout).with_entities(*selected_columns).all()
for row in record:
    print(row)
    result.append(row)

print(json.dumps(result))

session.close() 

但它不显示“col”模式下的数据。请建议,我该怎么办?

最佳答案

https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html#table-configuration

postgres 数据库的架构可以作为元组或字典通过 table_args 属性传递。

所以对于你的问题,你的类定义应该有这个额外的属性:

class Layout(Base):
    __tablename__ = "layout"
    __table_args__ = {"schema": "col"}
    layout_id = Column(UUID(as_uuid=True), nullable=False, primary_key=True)
    name = Column(String(1000), nullable=False)
    layout = Column(String(10000), nullable=False)
    grid_id = Column(Integer, nullable=False)
    user_id = Column(Integer, nullable=False)
    issystemlayout = Column(Integer, default=0, nullable=False)
    ispublic = Column(Integer, default=0, nullable=False)
    isactive = Column(Integer, default=0, nullable=False)
    createdby = Column(Integer, default=1, nullable=False)
    createdat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
    modifiedat = Column(TIMESTAMP, default=(datetime.datetime.now()), nullable=False)
    modifiedby = Column(Integer, default=1, nullable=False)

关于python - 如何通过指定模式从 postgresql 数据库中获取行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56127541/

相关文章:

python : How to call dictionnary-contained callables at a given time?

python - 使用 GSOC 进行 openCV 背景扣除

python - 插入重复键错误后 SELECT 失败

python - Flask 开发服务器部署在 Gcloud 上时出现服务器错误,但由于权限作为本地服务器工作正常

node.js - 快速保持连接打开?

python - sqlalchemy 关系映射

Python-将根目录名称与所有子文件夹名称连接起来

python - 写入然后读取内存字节 (BytesIO) 给出空白结果

PostgreSQL 向现有行添加更多值

java - JPA/Hibernate (PSQL) 不创建连接表