postgresql - 在 SQLAlchemy 中更新 PostgreSQL 数组字段

标签 postgresql sqlalchemy

考虑以下可标记模型:

from sqlalchemy import cast, Text
from sqlalchemy.dialects.postgresql import ARRAY, array


class User(Base):
    __tablename__ = 'users'

    tags = Column(ARRAY(Text), nullable=False,
                  default=cast(array([], type_=Text), ARRAY(Text)))

我似乎找不到有关如何更新该字段的任何文档。当然,我可以按照 Update a PostgreSQL array using SQLAlchemy 中的建议做一些事情。 :
user = session.query(User).get(1)
user.tags = ['abc', 'def', 'ghi']
session.add(user)
session.commit()

但该解决方案假定设置整个数组值。

如果我只想向数组附加一个值怎么办?如果我想批量标记一组User怎么办一个查询中的对象?我怎么做?

最佳答案

您可以使用 SQLAlchemy text和 PostgreSQL array_append职能:text('array_append(tags, :tag)')对于 smallint类型您可以使用 PostgreSQL 和 SQLAlchemy type castings :text('array_append(tags, :tag\:\:smallint)')TestTable.tags.contains(cast((1,), TestTable.tags.type))例子:

将值附加到整数 PostgreSQL 数组:

    from sqlalchemy.orm import sessionmaker, scoped_session
    from sqlalchemy import create_engine, Column, Integer, update, text
    from sqlalchemy.dialects.postgresql import ARRAY
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class TestTable(Base):
        __tablename__ = 'test_table'

        id = Column(Integer, primary_key=True)
        tags = Column(ARRAY(Integer), nullable=False)

    engine = create_engine('postgresql://postgres')
    Base.metadata.create_all(bind=engine)
    DBSession = scoped_session(sessionmaker())
    DBSession.configure(bind=engine)

    DBSession.bulk_insert_mappings(
        TestTable,
        ({'id': i, 'tags': [i // 4]} for i in range(1, 11))
    )

    DBSession.execute(
        update(
            TestTable
        ).where(
            TestTable.tags.contains((1,))
        ).values(tags=text(f'array_append({TestTable.tags.name}, :tag)')),
        {'tag': 100}
    )

    DBSession.commit()
将一个值附加到一个小的整数 PostgreSQL 数组:
    from sqlalchemy import SmallInteger, cast

    class TestTable(Base):
        __tablename__ = 'test_table2'

        id = Column(Integer, primary_key=True)
        tags = Column(ARRAY(SmallInteger), nullable=False)

    DBSession.execute(
        update(
            TestTable
        ).where(
            TestTable.tags.contains(cast((1,), TestTable.tags.type))
        ).values(tags=text(f'array_append({TestTable.tags.name}, :tag\:\:smallint)')),
        {'tag': 100}
    )
结果:
id |  tags   
----+---------
  1 | {0}
  2 | {0}
  3 | {0}
  8 | {2}
  9 | {2}
 10 | {2}
  4 | {1,100}
  5 | {1,100}
  6 | {1,100}
  7 | {1,100}

关于postgresql - 在 SQLAlchemy 中更新 PostgreSQL 数组字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127109/

相关文章:

PostgreSQL:如何在不延迟插入响应的情况下执行插入触发器?

django - 查询新的 Django/Postgres DateRangeField

python - SQLAlchemy是否支持 "closure tables?"

python - SQLAlchemy - 处理约束失败

postgresql - 真空满后释放内存?

postgresql - 插入(如果不存在)并返回 id

python - 如何检索 SQLAlchemy 结果集的 python 列表?

python - 使用 sqlalchemy create_engine 配置查询/命令超时?

python - Select 语句仅返回结果中的第一行

postgresql - 特定员工的最大日期和倒数第二个最大日期之间的差异 - postgresql