Operator DOES NOT ENTER WITH Peewee

The documentation here shows how to use the IN operator, but I could not find how to use the NOT IN operator.

If I put a not << , I get a syntax error.

If I put a not <FieldName> << , instead of WHERE (<FieldName> NOT IN (SELECT ... will be WHERE False .

Here is the result with sample documentation. The first is correct, the second and third are incorrect.

 >>> Tweet.select().where(Tweet.user << a_users).sql() ('SELECT t1."id", t1."user_id", t1."message", t1."created_date", t1."is_published" FROM "tweet" AS t1 WHERE (t1."user_id" IN (SELECT t2."id" FROM "user" AS t2 WHERE (Lower(Substr(t2."username", ?, ?)) = ?)))', [1, 1, 'a']) >>> Tweet.select().where(not Tweet.user << a_users).sql() ('SELECT t1."id", t1."user_id", t1."message", t1."created_date", t1."is_published" FROM "tweet" AS t1 WHERE ?', [False]) >>> Tweet.select().where(Tweet.user not << a_users).sql() SyntaxError: invalid syntax 
+7
python sqlite peewee
source share
3 answers

Plain:

 Tweet.select().where(Tweet.user.not_in(a_users)) 

For a slightly different semantics (NOT (x in y)) as opposed to (x NOT IN y):

 Tweet.select().where(~(Tweet.user << a_users)) 
+15
source share

I know this is "necro-posting", but this question first hit Google for a peewee not in request, so I would like to add it here:

You can also use the not_in method described in the document:

 Tweet.select().where(Tweet.user.not_in(a_users)) 

As for me, it looks much more readable than the design ~ ... << .

+2
source share

This has nothing to do with Peewee. Peewee uses some Python operators for its own purposes. << usually a numerical operator, and it makes no sense to accept its logical negation. Thus, not << never a valid Python syntax.

The second example is close, but not only applies to Tweet.user ( not with a higher priority than << ). Add a few brackets and you will get:

 Tweet.select().where(not (Tweet.user << a_users)).sql() 

Now this is still not correct , as you have discovered (readers: see comments on some discussions on this subject). not returns a boolean value that is not what is required and will not work. Peewee translates the ~ operator for this; take a look at @coleifer answer.

0
source share

All Articles