>
> You may get exceptions from the database driver if the id list is empty.
> Using Postgres, I do something like this:
>
> ids = db()._select(db.users_keywords.keyword_id, distinct=True)
> if len(ids):
> db((~db.keyword.id.belongs(ids)) & (db.keyword.dictionary ==
> dictionary)).
> delete()
>
Are you sure? Note, the ._select() doesn't generate a list of ids (which
could potentially be empty) -- it generates the SQL to select the ids and
is then used as part of a nested select. I'm not sure about all db's, but
this doesn't generate an error in SQLite even if there are no ids to
select. In any case, for the above to work, the ids query would have to be
a regular .select() rather than a ._select() (the latter will always have
length > 0 because it's just a SQL string). Rather than doing two separate
queries, another option is to put the delete inside a try/except:
try:
db((~db.keyword.id.belongs(ids)) & (db.keyword.dictionary == dictionary
)).delete()
except OperationalError:
pass
Anthony