Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#62 BoolCol() is allways True with BOOL or BOOLEAN column

closed-works-for-me
Oleg Broytman
SQLite (24)
5
2008-03-07
2005-03-07
No

I don't know if this is a bug or a feature, but is at
least odd. If the type of the column in a DB is BOOL or
BOOLEAN (or CHAR or other more weird types), and you
use a BoolCol() SQLObject column to represent it, the
value is allways True (I guess is using '0' as a string
and it evaluates to True).

If the column is TINYINT or INT, it works ok. Small
testcase attached.

Discussion

<< < 1 2 (Page 2 of 2)
  • Oleg Broytman
    Oleg Broytman
    2005-04-05

    Logged In: YES
    user_id=4799

    For boolean columns Postgres (PyGreSQL) returns strings 'f'
    and 't'. You cannot pass them to int().

     
  • Logged In: YES
    user_id=240225

    But this is never mapped to a Python bool() type?

     
  • Oleg Broytman
    Oleg Broytman
    2005-04-05

    Logged In: YES
    user_id=4799

    The following script:

    ----------
    import pg
    db = pg.connect("test")
    q = db.query("CREATE TABLE test (b boolean)")
    q = db.query("INSERT INTO test VALUES ('f')")
    q = db.query("INSERT INTO test VALUES ('t')")
    q = db.query("INSERT INTO test VALUES ('1')")
    q = db.query("SELECT * FROM test")
    r = q.getresult()
    print r[0][0]
    print type(r[0][0])
    ----------

    prints:

    ----------
    f
    <type 'str'>
    ----------

    Yes, PyGreSQL is not the most used driver. SQLObject is
    mostly oriented toward psycopg, but still PyGreSQL can be
    used instead.

     
  • Logged In: YES
    user_id=240225

    Ugh? I'm not talking about the connection object, I'm
    talking about SQLObject. I thought one of the goals of
    SQLObject was to unify this behavoir. So if tomorrow I want
    to change from SQLite to PostgreSQL I don't have to change
    all my test for BoolCol()s from True/False to 't'/'f'.

    Do you mean that if I have:
    class C(SQLObject):
    b = BoolCol()

    c = C.get(1)
    print c.b

    will print True/False (or 1/0) in SQLite and 't'/'f' in
    PostgreSQL?

    PS: Is there any IRC channel or other way to talk in a more
    interactive way?

     
  • Oleg Broytman
    Oleg Broytman
    2005-04-06

    Logged In: YES
    user_id=4799

    SQLObject should not be expected to compensate for user
    errors. If you use PyGreSQL, of if you use CHAR instead of
    INTEGER/TINYINT for boolean columns - why do SQLObject
    should fix it?

    PS. See http://sqlobject.org/ for details on the mailing list.

     
  • Oleg Broytman
    Oleg Broytman
    2008-03-07

    Logged In: YES
    user_id=4799
    Originator: NO

    I ran the test (slightly modified) with SQLOBject 0.9.4:

    print 'Expected:', False
    b = BoolTest(tinyint=False, integer=False, bool=False, boolean=False)
    print 'Got: tinyint =', b.tinyint, '| integer =', b.integer, '| bool =', b.bool, '| boolean = ', b.boolean

    print 'Expected:', True
    b = BoolTest(tinyint=True, integer=True, bool=True, boolean=True)
    print 'Got: tinyint =', b.tinyint, '| integer =', b.integer, '| bool =', b.bool, '| boolean = ', b.boolean

    Output:

    1/Query :
    CREATE TABLE bool_test (
    id INTEGER PRIMARY KEY,
    tinyint TINYINT,
    integer INTEGER,
    bool BOOL,
    boolean BOOLEAN
    )
    1/QueryR :
    CREATE TABLE bool_test (
    id INTEGER PRIMARY KEY,
    tinyint TINYINT,
    integer INTEGER,
    bool BOOL,
    boolean BOOLEAN
    )
    Expected: False
    2/QueryIns: INSERT INTO bool_test (integer, tinyint, boolean, bool) VALUES (0, 0, 0, 0)
    2/QueryR : INSERT INTO bool_test (integer, tinyint, boolean, bool) VALUES (0, 0, 0, 0)
    3/QueryOne: SELECT tinyint, integer, bool, boolean FROM bool_test WHERE ((bool_test.id) = (1))
    3/QueryR : SELECT tinyint, integer, bool, boolean FROM bool_test WHERE ((bool_test.id) = (1))
    Got: tinyint = False | integer = False | bool = False | boolean = False
    Expected: True
    4/QueryIns: INSERT INTO bool_test (integer, tinyint, boolean, bool) VALUES (1, 1, 1, 1)
    4/QueryR : INSERT INTO bool_test (integer, tinyint, boolean, bool) VALUES (1, 1, 1, 1)
    5/QueryOne: SELECT tinyint, integer, bool, boolean FROM bool_test WHERE ((bool_test.id) = (2))
    5/QueryR : SELECT tinyint, integer, bool, boolean FROM bool_test WHERE ((bool_test.id) = (2))
    Got: tinyint = True | integer = True | bool = True | boolean = True

     
  • Oleg Broytman
    Oleg Broytman
    2008-03-07

    • status: open --> closed-works-for-me
     
<< < 1 2 (Page 2 of 2)