Thread: [SQLObject] BUG: ForeignKey in another database
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: J-P L. <sql...@si...> - 2003-11-17 23:59:07
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> <title></title> </head> <body> Hi All,<br> <br> I just switched from v0.4 to v0.51 and my app broke. It looks like there's a bug in the current release. I'm doing this:<br> <br> <u>Common.py<br> </u>__connection__ = PostgresConnection('dbname=common ...')<br> class User(SQLObject):<br> name = StringCol()<br> ...<br> <br> <u>App.py</u><br> __connection__ = PostgresConnection('dbname=app ...')<br> from Common import User<br> <br> class Notes(SQLObject):<br> ...<br> user = ForeignKey('User')<br> ...<br> <br> This use to work and I use to be able to do Notes(1).user.name. Now I get the following error:<br> <br> Traceback (most recent call last):<br> File "../../external/lib/python/SQLObject/SQLObject.py", line 815, in _SO_foreignKey<br> File "../../external/lib/python/SQLObject/SQLObject.py", line 407, in __new__<br> File "../../external/lib/python/SQLObject/SQLObject.py", line 667, in _init<br> File "../../external/lib/python/SQLObject/DBConnection.py", line 306, in _SO_selectOne<br> File "../../external/lib/python/SQLObject/DBConnection.py", line 149, in queryOne<br> File "../../external/lib/python/SQLObject/DBConnection.py", line 72, in _runWithConnection<br> File "../../external/lib/python/SQLObject/DBConnection.py", line 142, in _queryOne<br> psycopg.ProgrammingError: ERROR: Relation "user" does not exist<br> <br> SELECT name, ... FROM user WHERE id = xx<br> <br> Is there another way to do this?<br> <br> Thanks,<br> <br> J-P<br> </body> </html> |
From: Ian B. <ia...@co...> - 2003-11-19 01:23:12
|
On Nov 17, 2003, at 5:59 PM, J-P Lee wrote: > Hi All, > > I just switched from v0.4 to v0.51 and my app broke. It looks like=20 > there's a bug in the current release. =A0 I'm doing this: > > Common.py > __connection__ =3D PostgresConnection('dbname=3Dcommon ...') > class User(SQLObject): > =A0=A0=A0 name =3D StringCol() > =A0=A0=A0 ... > > App.py > __connection__ =3D PostgresConnection('dbname=3Dapp ...') > from Common import User > > class Notes(SQLObject): > =A0=A0=A0 ... > =A0=A0=A0 user =3D ForeignKey('User') > =A0=A0=A0 ... > > This use to work and I use to be able to do Notes(1).user.name.=A0 Now = I=20 > get the following error: > > Traceback (most recent call last): > =A0 File "../../external/lib/python/SQLObject/SQLObject.py", line 815,=20= > in _SO_foreignKey > =A0 File "../../external/lib/python/SQLObject/SQLObject.py", line 407,=20= > in __new__ > =A0 File "../../external/lib/python/SQLObject/SQLObject.py", line 667,=20= > in _init > =A0 File "../../external/lib/python/SQLObject/DBConnection.py", line=20= > 306, in _SO_selectOne > =A0 File "../../external/lib/python/SQLObject/DBConnection.py", line=20= > 149, in queryOne > =A0 File "../../external/lib/python/SQLObject/DBConnection.py", line = 72,=20 > in _runWithConnection > =A0 File "../../external/lib/python/SQLObject/DBConnection.py", line=20= > 142, in _queryOne > psycopg.ProgrammingError: ERROR:=A0 Relation "user" does not exist > > SELECT name, ... FROM user WHERE id =3D xx > > Is there another way to do this? Hmm... I'm not really sure. It sounds like you haven't created the=20 table like you should have. Though I also feel like "user" is a=20 reserved word, so it shouldn't even work anyway. To test -- what=20 happens when you do __connection__.queryAll('SELECT * FROM user')? -- Ian Bicking | ia...@co... | http://blog.ianbicking.org |
From: J-P L. <sql...@si...> - 2003-11-19 16:26:51
|
OK, I've narrowed this down. It works fine when accessing the records directly using Notes(1).user.name. However, when iterating over a query result, it fails. for rec in Notes.select(): print rec.user.name I'm looking at the differences between the versions. With the latest release, conn is being passed along whereas in the previous release it's retrieved from the instance. I assume all this connection passing was added to resolve transaction problems. Is there a way to make iterSelect work using 2 databases? Ian Bicking wrote: > Hmm... I'm not really sure. It sounds like you haven't created the > table like you should have. Though I also feel like "user" is a > reserved word, so it shouldn't even work anyway. To test -- what > happens when you do __connection__.queryAll('SELECT * FROM user')? |
From: Ian B. <ia...@co...> - 2003-11-19 18:16:28
|
On Nov 19, 2003, at 10:27 AM, J-P Lee wrote: > OK, I've narrowed this down. It works fine when accessing the records > directly using Notes(1).user.name. However, when iterating over a > query result, it fails. > > for rec in Notes.select(): > print rec.user.name > > I'm looking at the differences between the versions. With the latest > release, conn is being passed along whereas in the previous release > it's retrieved from the instance. I assume all this connection > passing was added to resolve transaction problems. Is there a way to > make iterSelect work using 2 databases? Mmm... I guess it was an unintended feature of 0.4 that you could do cross-database joins. I'm guessing you could do: for rec in Notes.select(): user = User(rec.userID) print user.name or: class Notes(SQLObject): .... def _get_user(self): return User(self.userID) Probably the best way to handle this would be to separate transactions from databases and connections. That is, you'd have one transactional context, which would potentially connect to multiple databases, and multiple specific database connections. Then you'd attach classes to a database, and this attachment would be fairly strong (stronger than currently, where now you can suddenly "move" an instance to another database by passing in a connection= parameter). Anyway, that's what should happen to SQLObject in the future, and it might produce cleaner code, because transactions are kind of wonky right now. But for now a workaround will have to do. -- Ian Bicking | ia...@co... | http://blog.ianbicking.org |