如何使用 postgres 在 peewee 查询中访问 ArrayField 的最后一个索引?
How do I access the last index of an ArrayField in a peewee query with postgres?
我有一个带有数组字段的模型,我想查找字段最后一个元素大于已知值的记录。数组字段将包含不同长度的数组。
我认为对模型 {Test} 使用类似 Test.select().where(known_value <= Test.array_field[-1])
的方法会起作用,但 {-1} 似乎没有按预期起作用。这是一个最低限度的工作示例:
from os import path, environ
import peewee as pw
from playhouse.postgres_ext import ArrayField
from db_stuff import connect, get_database
class Test(pw.Model):
# note an 'id' primary key field will be created by peewee by default
value = ArrayField(pw.IntegerField)
class Meta:
table_name = 'test2'
# connects the model to the database and returns the instance, details are
# unimportant
connect('test_db_login.json', [Test])
database = get_database()
database.create_tables([Test])
Test.create(value=[1,2,3,4])
Test.create(value=[5,6,7,8])
known_value = 6
for entry in Test.select():
# outputs both records
print('id', entry.id, 'value', entry.value)
for entry in Test.select().where(Test.value[0] <= known_value):
# outputs both records
print('id', entry.id, 'value', entry.value, 'first value', entry.value[0])
for entry in Test.select().where(known_value <= Test.value[-1]):
# outputs nothing
print('id', entry.id, 'value', entry.value, 'last value', entry.value[-1])
当我在 table 上尝试原始 sql 查询时,事情按预期进行:SELECT "t1"."id", "t1"."value" FROM "test2" AS "t1" WHERE ("t1"."value"[ARRAY_UPPER("t1"."value", 1)] >= 6);
,返回第二条记录。
我在 peewee 代码中看了一堆,并在网上搜索了一种机制,其功能类似于 peewee 中的 ARRAY_UPPER
,但没有成功。
这里有什么建议吗?
您应该按照您的指示使用 fn.array_upper(Test.value, 1)
等表达式进行索引。
已由 55ef182840f869f63 在 master 中修复,将在下一个版本中提供。
我有一个带有数组字段的模型,我想查找字段最后一个元素大于已知值的记录。数组字段将包含不同长度的数组。
我认为对模型 {Test} 使用类似 Test.select().where(known_value <= Test.array_field[-1])
的方法会起作用,但 {-1} 似乎没有按预期起作用。这是一个最低限度的工作示例:
from os import path, environ
import peewee as pw
from playhouse.postgres_ext import ArrayField
from db_stuff import connect, get_database
class Test(pw.Model):
# note an 'id' primary key field will be created by peewee by default
value = ArrayField(pw.IntegerField)
class Meta:
table_name = 'test2'
# connects the model to the database and returns the instance, details are
# unimportant
connect('test_db_login.json', [Test])
database = get_database()
database.create_tables([Test])
Test.create(value=[1,2,3,4])
Test.create(value=[5,6,7,8])
known_value = 6
for entry in Test.select():
# outputs both records
print('id', entry.id, 'value', entry.value)
for entry in Test.select().where(Test.value[0] <= known_value):
# outputs both records
print('id', entry.id, 'value', entry.value, 'first value', entry.value[0])
for entry in Test.select().where(known_value <= Test.value[-1]):
# outputs nothing
print('id', entry.id, 'value', entry.value, 'last value', entry.value[-1])
当我在 table 上尝试原始 sql 查询时,事情按预期进行:SELECT "t1"."id", "t1"."value" FROM "test2" AS "t1" WHERE ("t1"."value"[ARRAY_UPPER("t1"."value", 1)] >= 6);
,返回第二条记录。
我在 peewee 代码中看了一堆,并在网上搜索了一种机制,其功能类似于 peewee 中的 ARRAY_UPPER
,但没有成功。
这里有什么建议吗?
您应该按照您的指示使用 fn.array_upper(Test.value, 1)
等表达式进行索引。
已由 55ef182840f869f63 在 master 中修复,将在下一个版本中提供。