Bugs item #1158024, was opened at 2005-03-07 04:49
Message generated for change (Comment added) made by phd
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=540672&aid=1158024&group_id=74338
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: SQLite
Group: SQLObject from repository
>Status: Closed
>Resolution: Works For Me
Priority: 5
Private: No
Submitted By: Leandro Lucarella (llucax)
Assigned to: Oleg Broytmann (phd)
Summary: BoolCol() is allways True with BOOL or BOOLEAN column
Initial Comment:
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.
----------------------------------------------------------------------
>Comment By: Oleg Broytmann (phd)
Date: 2008-03-07 17:59
Message:
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
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-04-06 07:53
Message:
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.
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-04-06 06:12
Message:
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?
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-04-05 19:26
Message:
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.
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-04-05 19:04
Message:
Logged In: YES
user_id=240225
But this is never mapped to a Python bool() type?
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-04-05 18:38
Message:
Logged In: YES
user_id=4799
For boolean columns Postgres (PyGreSQL) returns strings 'f'
and 't'. You cannot pass them to int().
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-03-30 17:09
Message:
Logged In: YES
user_id=240225
It won't hurt MySQL or Postgres. If Postgres return a
bool(), when converted to int() and evaluated as bool()
again, it remains the same, same for MySQL int(). I think is
harmless for other DB engines.
The problem is I'm using the DB in other programs too, not
just SQLObject.
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-03-30 16:00
Message:
Logged In: YES
user_id=4799
The patch is meaningful only for SQLite. What about
Postgres? MySQL?
When you create a DB by hand create it according SQLObject
rules. The boolean columns in SQLite must be TINYINT.
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-03-30 06:39
Message:
Logged In: YES
user_id=240225
Here's a small workarround that converts to python type
int() before evaluating to True or False in
BoolValidator.toPython().
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-03-30 06:27
Message:
Logged In: YES
user_id=240225
I don't. I have the column created as a BOOL type. Since
SQLite is typeless its seems to be mapped to simple string
by... pysqlite?
I'm creating the database by hand.
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-03-30 00:03
Message:
Logged In: YES
user_id=4799
Why do you have a string in the BoolCol? SOBoolCol declares
def _sqliteType(self): return "TINYINT"
----------------------------------------------------------------------
Comment By: Leandro Lucarella (llucax)
Date: 2005-03-29 23:36
Message:
Logged In: YES
user_id=240225
There's no way to make SQLite to convert BoolCol() the
(possible) string() value to int() before evaluate them as
bool()?
Or this is an PySQLite issue? If so, is there any way to use
PySQLite type mapping to fixe it?
I just couldn't find where to do it but if you give me a
clue I can try to fix it my self...
----------------------------------------------------------------------
Comment By: Oleg Broytmann (phd)
Date: 2005-03-29 20:19
Message:
Logged In: YES
user_id=4799
MySQL maps BOOL to TINYINT, which is much more correct
mapping. Postgres has a special BOOLEAN column type.
SQLite does not have BOOL/BOOLEAN type. I don't know to what
type it's silently mapped. Probably, CHAR. SQLite is a bad
citizen here. Use INT/TINYINT instead.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=540672&aid=1158024&group_id=74338
|