#216 Can not set ForeignKey and Unicode cols at the same time.

closed-invalid
Oleg Broytman
None
5
2007-04-10
2007-04-08
No

on SQLObject-0.8.1 py2.4

Can not set ForeignKey and Unicode cols at the same time.

test code::

class Foo(SQLObject):
name = UnicodeCol()

class User(SQLObject):
name = UnicodeCol()
foo = ForeignKey('Foo')

foo1 = Foo(name="Foo1")
foo2 = Foo(name="Foo2")

# raise UnicodeDecodeError
user = User(name=u'\u8ab0\u304b', fooID=u'2')

# raise UnicodeDecodeError
user.set(name=u'\u8ab0\u304b', fooID=u'2')

Discussion

  • Oleg Broytman
    Oleg Broytman
    2007-04-10

    • assigned_to: nobody --> phd
    • status: open --> closed-invalid
     
  • Oleg Broytman
    Oleg Broytman
    2007-04-10

    Logged In: YES
    user_id=4799
    Originator: NO

    You are passing unicode to the ForeignKey column: fooID=u'2'. You have to pass ID-type (default is integer). The following program works for me:

    from sqlobject import *

    __connection__ = "sqlite:/:memory:?debug=1"

    class Foo(SQLObject):
    name = UnicodeCol()

    class User(SQLObject):
    name = UnicodeCol()
    foo = ForeignKey('Foo')

    Foo.createTable()
    User.createTable()

    foo1 = Foo(name="Foo1")
    foo2 = Foo(name="Foo2")

    user = User(name=u'\u8ab0\u304b', fooID=2)
    user.set(name=u'\u8ab0\u304b', fooID=2)

    Output:

    1/Query : CREATE TABLE foo (
    id INTEGER PRIMARY KEY,
    name TEXT
    )
    1/QueryR : CREATE TABLE foo (
    id INTEGER PRIMARY KEY,
    name TEXT
    )
    2/Query : CREATE TABLE user (
    id INTEGER PRIMARY KEY,
    name TEXT,
    foo_id INT CONSTRAINT foo_id_exists REFERENCES foo(id)
    )
    2/QueryR : CREATE TABLE user (
    id INTEGER PRIMARY KEY,
    name TEXT,
    foo_id INT CONSTRAINT foo_id_exists REFERENCES foo(id)
    )
    3/QueryIns: INSERT INTO foo (name) VALUES ('Foo1')
    3/QueryR : INSERT INTO foo (name) VALUES ('Foo1')
    4/QueryOne: SELECT name FROM foo WHERE id = (1)
    4/QueryR : SELECT name FROM foo WHERE id = (1)
    5/QueryIns: INSERT INTO foo (name) VALUES ('Foo2')
    5/QueryR : INSERT INTO foo (name) VALUES ('Foo2')
    6/QueryOne: SELECT name FROM foo WHERE id = (2)
    6/QueryR : SELECT name FROM foo WHERE id = (2)
    7/QueryIns: INSERT INTO user (foo_id, name) VALUES (2, '誰か')
    7/QueryR : INSERT INTO user (foo_id, name) VALUES (2, '誰か')
    8/QueryOne: SELECT name, foo_id FROM user WHERE id = (1)
    8/QueryR : SELECT name, foo_id FROM user WHERE id = (1)
    9/Query : UPDATE user SET foo_id = (2), name = ('誰か') WHERE id = (1)
    9/QueryR : UPDATE user SET foo_id = (2), name = ('誰か') WHERE id = (1)