Nested in_ inside not_ fails with just one parameter on sqlalchemy 0.3.10

Issue #764 resolved
Former user created an issue

When nesting an in_ clause inside a not_ and having only one parameter, sqlalchemy 0.3.8 puts parenthesis around the clause, whereas 0.3.10 does not. This fails for mysql4 (MyIsam) but works for postgres. It fails because sqlalchemy 0.3.10 optimizes a single in_(1) to an "=", and somehow mysql is not happy with "not something = 1". 0.4.0 trunk does not have this problem because it optimizes not_(in_(1)) => "!=".

Test script:

from sqlalchemy import (
    Table, Column, MetaData,
    ForeignKey, Integer,
    Sequence,
    not_,
)

meta = MetaData()
meta.connect('mysql://user:passwd@myserver/mydb')

foobar = Table('foobar', meta,
    Column('foobar_id', Integer, Sequence('foobar_seq'), primary_key=True),
)

try:
    foobar.create()
    foobar.insert().execute()
    foobar.insert().execute()
    qry = foobar.select(
        not_(foobar.c.foobar_id.in_(1)),
    )
    assert len(qry.execute().fetchall()) == 1
finally:
    foobar.drop()

SQL of 0.3.8

SELECT foobar.foobar_id 
FROM foobar 
WHERE NOT (foobar.foobar_id = %s)

SQL of 0.3.10

SELECT foobar.foobar_id 
FROM foobar 
WHERE NOT foobar.foobar_id = %s

Exception:

Traceback (most recent call last):
  File "sqlalchemy_newbug.py", line 22, in <module>
    assert len(qry.execute().fetchall()) == 1
AssertionError

Comments (1)

  1. Log in to comment