Here is a simple Peewee model to work with Postgres
import playhouse.postgres_ext as pg
db = pg.PostgresqlDatabase(""" credentials ... """)
class Artist(pg.Model):
name = pg.TextField()
albums = pg.ArrayField(pg.TextField, default=[])
class Meta:
database = db
db.create_table(Artist)
Artist.create(name='name1', albums=['album11', 'album12'])
Artist.create(name='name2')
For selecting artists with no albums the SQL query may be
>> SELECT * FROM artist WHERE albums = '{}';
id | name | albums
----+-------+--------
2 | name2 | {}
or for selecting artists with specific name
>> SELECT * FROM artist WHERE name = 'name1';
id | name | albums
----+-------+-------------------
1 | name1 | {album11,album12}
But when I try to achieve it with Peewee I get the following results
res = Artist.select().where(Artist.name == 'name1')
assert len(res) == 1 and res[0].name == 'name1'
res = Artist.select().where(Artist.albums == '{}')
assert len(res) == 0
The first query takes 'name1'
as query parameter.
The second query takes playhouse.postgres_ext._Array
object as query parameter.
I've looked at documentation on Postgres Extensions and did not find anything suitable.
Could someone explain what am I doing wrong and how to select data with empty array field?
You can drop into raw SQL to work around this limitation:
res = Artist.select().where(SQL("albums = '{}'"))
print(len(res))