Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQLAlchemy选择PostgreSQL系统列?_Sql_Postgresql_Sqlalchemy - Fatal编程技术网

如何使用SQLAlchemy选择PostgreSQL系统列?

如何使用SQLAlchemy选择PostgreSQL系统列?,sql,postgresql,sqlalchemy,Sql,Postgresql,Sqlalchemy,Postgresql隐式地在每个表上定义了若干列,如xmax和ctid see 假设我的SQLALchemy表定义没有指定这些列,有没有办法使用核心sql功能(即不是SA的ORM部分)来选择它们 由于表定义中未显式定义xmax,因此以下内容不起作用 table=sa.table 我的桌子, 元数据, 萨科鲁阿,萨比金, sa.日期,sa.日期, s=sa.select[table.c.xmax] 结果=engine.executes 具体来说,我的要求是在upsert的returning子句中引

Postgresql隐式地在每个表上定义了若干列,如xmax和ctid see

假设我的SQLALchemy表定义没有指定这些列,有没有办法使用核心sql功能(即不是SA的ORM部分)来选择它们

由于表定义中未显式定义xmax,因此以下内容不起作用

table=sa.table 我的桌子, 元数据, 萨科鲁阿,萨比金, sa.日期,sa.日期, s=sa.select[table.c.xmax] 结果=engine.executes 具体来说,我的要求是在upsert的returning子句中引用xmax

insertmytable.returningmytable.c.xmax==0.labelinserted 一个选项是将SA表定义中的xmax声明为系统列:

table=sa.table ..., sa.Columnxmax,sa.TEXT,system=True, 这将允许您像访问任何其他列一样访问table.c.xmax,因此建议的table.c.xmax==0应该可以工作。system=True标志告诉SA在发出create TABLE时不要尝试显式创建xmax列


我在这里使用sa.TEXT作为一种变通方法,因为sqlalchemy.Dialogons.postgresql不提供XID数据类型,显然XID不能转换为数字类型。

如果不想更改现有的表声明,可以使用函数注意列中的小写c:

xmax=sa.列'xmax' sa.insertmytable.returningxmax==0.labelinserted 但是,如果您的SQL语句从多个表中进行选择(例如在联接中),则PostgreSQL会抱怨它不知道您所说的是哪个xmax列:

ProgrammingError: (psycopg2.ProgrammingError) column "xmax" does not exist
在这种情况下,您可以使用不幸未记录的可选参数:

xmax = sa.column('xmax', _selectable=table)

如果您只从一个表中进行选择,该方法同样有效。

我现在记得,我可以将文字sql作为字符串提供,并避开这个问题,尽管我对任何其他方法都感兴趣。insertmytable.returningsa.textxmax=0作为InsertedHanks这是一个有趣的答案。我不知道列上的系统标志