Thread: [SQLObject] Hi guys. Please, I need help
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: fingerclick <fc...@bo...> - 2004-04-21 18:30:41
|
I´d like to put in one table two references do a same table. Does sqlObject allows that? Or may i build the sql in another way to make that works? db_login has the names and db_request has 2 fields that point to db_login( these fields are the persons id ).Thanks in advance. The tables: CREATE TABLE db_login ( id SERIAL PRIMARY KEY, login TEXT NOT NULL, password TEXT NOT NULL, ativo BOOLEAN DEFAULT true, name TEXT ); CREATE TABLE db_request ( id SERIAL PRIMARY KEY, new_file BOOLEAN DEFAULT true, db_request_id INTEGER NOT NULL REFERENCES db_login (id), db_source_id INTEGER NOT NULL REFERENCES db_login (id), ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); |
From: Scott R. <sc...@to...> - 2004-04-21 18:43:03
|
On Wed, 2004-04-21 at 14:17, fingerclick wrote: > I=B4d like to put in one table two references do a same table. Does = > sqlObject allows that? Or may i build the sql in another way to > make that works? db_login has the names and db_request has 2 fields tha= t = > point to db_login( these fields are the persons id ).Thanks in advance.= Unless I misunderstand the problem, if you take the time to discover how to add one reference field, it is the exact same process to add another. class request(SQLObject): NewFile =3D BoolCol(default=3D1) DbRequestId =3D ForeignKey('Login') DbSourceId =3D ForeignKey('Login') ts =3D DateTimeCol() |
From: fingerclick <fc...@bo...> - 2004-04-22 04:36:53
|
Scott Russell wrote: > On Wed, 2004-04-21 at 14:17, fingerclick wrote: > >>I´d like to put in one table two references do a same table. Does >>sqlObject allows that? Or may i build the sql in another way to >>make that works? db_login has the names and db_request has 2 fields that >>point to db_login( these fields are the persons id ).Thanks in advance. > > > Unless I misunderstand the problem, if you take the time to discover how > to add one reference field, it is the exact same process to add another. > > class request(SQLObject): > NewFile = BoolCol(default=1) > DbRequestId = ForeignKey('Login') > DbSourceId = ForeignKey('Login') > ts = DateTimeCol() > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President and CEO of > GenToo technologies. Learn everything from fundamentals to system > administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click Thanks man. But i think i still have a problem. The "DbRequestId" and the "DbSourceId" fields are linking to table Login. So, wouldn't DbRequestId and DbSourceId have to be renamed to Login? So, this is the point: i have two fields pointing to the same table. Take a look at my code: from SQLObject import * __connection__ = PostgresConnection('user=my_user dbname=my_user') class Login(SQLObject): firstName = StringCol() middleInitial = StringCol(length=1, default=None) lastName = StringCol() addresses = MultipleJoin('db_request') class db_request(SQLObject): new_file = BoolCol(default=1) DbRequestId = ForeignKey('Login') DbSourceId = ForeignKey('Login') def reset(): Login.dropTable(ifExists=True) Login.createTable() db_request.dropTable(ifExists=True) db_request.createTable() reset() p1 = Login.new(firstName='John', lastName='Doe') p2 = Login.new(firstName='Cal', lastName='Robson') r1 = db_request.new( new_file='t', DbRequestId=p1, DbSourceId=p2 ) -> And it reported the following error: Traceback (most recent call last): File "tt.py", line 27, in ? r1 = db_request.new( new_file='t', DbRequestId=p1, DbSourceId=p2 ) File "/usr/lib/python2.3/site-packages/SQLObject/SQLObject.py", line 907, in new inst._SO_finishCreate(id, connection=connection) File "/usr/lib/python2.3/site-packages/SQLObject/SQLObject.py", line 929, in _SO_finishCreate id, names, values) File "/usr/lib/python2.3/site-packages/SQLObject/DBConnection.py", line 163, in queryInsertID return self._runWithConnection(self._queryInsertID, table, idName, id, names, values) File "/usr/lib/python2.3/site-packages/SQLObject/DBConnection.py", line 74, in _runWithConnection val = meth(conn, *args) File "/usr/lib/python2.3/site-packages/SQLObject/DBConnection.py", line 629, in _queryInsertID q = self._insertSQL(table, names, values) File "/usr/lib/python2.3/site-packages/SQLObject/DBConnection.py", line 155, in _insertSQL return ("INSERT INTO %s (%s) VALUES (%s)" % File "/usr/lib/python2.3/site-packages/SQLObject/DBConnection.py", line 348, in sqlrepr return sqlrepr(v, self.dbName) File "/usr/lib/python2.3/site-packages/SQLObject/Converters.py", line 177, in sqlrepr raise ValueError, "Unknown SQL builtin type: %s for %s" % \ ValueError: Unknown SQL builtin type: <class '__main__.Login'> for <Login 2 lastName='Robson' middleInitial=None firstName='Cal'> Thanks in advance for all help, im a little confused about this and i really need it. : ( |
From: Scott R. <sc...@to...> - 2004-04-22 10:15:32
|
You need to let SQLobject do the work. :) class Login(SQLObject): firstName = StringCol() middleInitial = StringCol(length=1, default=None) lastName = StringCol() class db_request(SQLObject): new_file = IntCol(default=1) DbRequest = ForeignKey('Login') DbSource = ForeignKey('Login') ... snip ... r1 = db_request.new(new_file=1, DbRequest=p1, DbSource=p2 ) Notice all I did was remove "Id" from the end of your column names. Fields ending in _id are special items to SQLobject. Besides, you didn't want to reference the id - you wanted to reference the object. Let SO worry about the IDs, unless you're doing something especially tricky. I also flipped the BoolCol to an IntCol because I've never used it and I couldn't figure out how to. :) (Is BoolCol a postgres-only column?) - Scott |
From: Ian B. <ia...@co...> - 2004-04-22 15:52:32
|
Scott Russell wrote: > I also flipped the BoolCol to an IntCol because I've never used it and I > couldn't figure out how to. :) (Is BoolCol a postgres-only column?) BoolCol should work anywhere, it will just end up as a TINYINT or INT on database that don't have BOOLEAN (which is everything but Postgres). It also coerces non-integers into booleans, so it's better than just an IntCol. Ian |
From: fingerclick <fc...@bo...> - 2004-04-23 22:13:21
|
Scott Russell wrote: > You need to let SQLobject do the work. :) > > class Login(SQLObject): > firstName = StringCol() > middleInitial = StringCol(length=1, default=None) > lastName = StringCol() > > class db_request(SQLObject): > new_file = IntCol(default=1) > DbRequest = ForeignKey('Login') > DbSource = ForeignKey('Login') > > ... snip ... > > r1 = db_request.new(new_file=1, DbRequest=p1, DbSource=p2 ) > > Notice all I did was remove "Id" from the end of your column names. > Fields ending in _id are special items to SQLobject. Besides, you > didn't want to reference the id - you wanted to reference the object. > Let SO worry about the IDs, unless you're doing something especially > tricky. > > I also flipped the BoolCol to an IntCol because I've never used it and I > couldn't figure out how to. :) (Is BoolCol a postgres-only column?) > > - Scott > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President and CEO of > GenToo technologies. Learn everything from fundamentals to system > administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click Thanks to much man for all help. :) Now i´m stating to inderstand he way SQLObject works, and i think it´s my right choice. |