Thread: [SQLObject] cannot insert with Sybase
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <wzi...@co...> - 2009-03-03 17:55:44
|
Hello, ... has anyone gotten SQLObject to work with Sybase? I can query the database but I can't insert. Any advice would be appreciated. The table already exists and is extremely simple, with just two columns, a key and a text string. Here's the class for the table: ========================================== from sqlobject import * class titles( SQLObject ): title_id = IntCol() title = StringCol() class sqlmeta: print 'class sqlmeta' idName = 'title_id' lazyUpdate = False autoCommit = True ========================================== And here's a separate class where I attempt to insert: ========================================== from sqlobject import * import titles class InsertNewTitle: def __init__( self, newKey, newTitle ): sybURI = "sybase://my_user_id:my_password@DBSERVER//library_info" konnection = connectionForURI( sybURI ) sqlhub.processConnection = konnection book_a = titles.titles(title_id=0, title='zero') book_a._connection.debug = True book_a._connection.autoCommit = True book_b = titles.titles(title_id=newKey, title=newTitle) book_b._connection.debug = True book_b._connection.autoCommit = True book_b.id = newKey book_b.set(title_id=newKey, title = newTitle) book_b.syncUpdate() print 'for ID %d the title is %s' % ( book_b.title_id, book_b.title ) return None if __name__ == "__main__": instantiatedObject = InsertNewTitle(506,"Spy Story") ========================================== This code actually appears to work with no errors, but I can run it many times with no duplicate rows because nothing gets inserted into the database. I tried wrapping it all in a transaction and doing a commit, but that didn't help. It's not a permission problem because I've used the same ID and password in an SQL tool and I can insert. Note I instantiate two books. I don't want to, but I'm forced to because if I try to give it a non-zero first key I get the error: "sqlobject.main.SQLObjectNotFound: The object titles by the ID 0 does not exist" ....even though I'm providing a non-zero key! So I have 2 questions, if anyone would be kind enough to help: why does it complain about 0 not existing when I create only one book using a non-zero key (but doesn't complain as long as I use the 2 book work-around shown) and why does nothing get written to the database, even when I put in a commit? Thanks in advance for any help. .....WZ |
From: Oleg B. <ph...@ph...> - 2009-03-03 18:18:24
|
On Tue, Mar 03, 2009 at 05:55:43PM +0000, wzi...@co... wrote: > class sqlmeta: > print 'class sqlmeta' > idName = 'title_id' > lazyUpdate = False > autoCommit = True sqlmeta doesn't have 'autoCommit' attribute. (It'd also be helpful to send python code properly formatted.) > book_a._connection.debug = True > book_a._connection.autoCommit = True It is too late to set this after creating a row. The settings should be set on the 'konnection' before inserting. > This code actually appears to work with no errors, but I can run it > many times with no duplicate rows because nothing gets inserted into > the database. No commit has been done. > I tried wrapping it all in a transaction and doing a > commit, but that didn't help. It'd be interesting to see the code. > It's not a permission problem Certainly not - with a permission problem you'd get an exception. > Note I instantiate two books. I don't want to, but I'm forced to > because if I try to give it a non-zero first key I get the error: > "sqlobject.main.SQLObjectNotFound: The object titles by the ID 0 > does not exist" ....even though I'm providing a non-zero key! Please show the debugging output and the entire traceback. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: <wzi...@co...> - 2009-03-04 10:31:34
|
Hello Oleg and others, Thanks sincerely for the help; I'm much further along because now rows are inserted successfully. However, I still get an odd error message referring to key 0 when I do a simple print to show the values in the object. BTW, I will do my best to preserve indenting but my web-based email service is determined to ruin the code. better class 1: =========================================================== from sqlobject import * class titles( SQLObject ): title_id = IntCol() title = StringCol() class sqlmeta: print 'class sqlmeta' idName = 'title_id' lazyUpdate = False =========================================================== better class 2: =========================================================== from sqlobject import * import titles class InsertNewTitle: def makeNewBook( self ): sybURI = "sybase://my_user_id:my_password@DBSERVER//library_info" konnection = connectionForURI( sybURI ) konnection.debug = True konnection.autoCommit = True sqlhub.processConnection = konnection newKey = 1234 newTitle = "Mystery Book" book_b = titles.titles(title_id=newKey, title=newTitle) # works! # book_b.id = newKey # book_b.set(title_id=newKey, title = newTitle) # book_b.syncUpdate() # book_b = titles.get(newKey) print 'for ID %d the title is %s' % ( book_b.title_id, book_b.title ) # fails return None if __name__ == "__main__": instantiatedObject = InsertNewTitle() instantiatedObject.makeNewBook() =========================================================== and the error and the messages I cannot prevent are: =========================================================== class sqlmeta QueryIns: INSERT INTO titles (title_id, title) VALUES (1234, 'Mystery Book') 1/QueryOne: SELECT title_id, title FROM titles WHERE ((titles.title_id) = (0)) 1/QueryR : SELECT title_id, title FROM titles WHERE ((titles.title_id) = (0)) Traceback (most recent call last): File "/diska/data/workspace/PyXtern/src/tmpTry/InsertNewTitle.py", line 30, in <module> instantiatedObject.makeNewBook() File "/diska/data/workspace/PyXtern/src/tmpTry/InsertNewTitle.py", line 18, in makeNewBook book_b = titles.titles(title_id=newKey, title=newTitle) File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1203, in __init__ File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1251, in _create File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1278, in _SO_finishCreate File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 931, in _init sqlobject.main.SQLObjectNotFound: The object titles by the ID 0 does not exist =========================================================== Note the commented code showing ways I have tried to persuade the object to think about my new key, but clearly it remains focused on key 0 which does not exist in the database, so the commented lines did not help. What might be the appropriate remedy for this error? Again thank you for the timely help, .....WZ |
From: Oleg B. <ph...@ph...> - 2009-03-04 10:56:01
|
On Wed, Mar 04, 2009 at 10:31:31AM +0000, wzi...@co... wrote: > from sqlobject import * > > class titles( SQLObject ): > > title_id = IntCol() > title = StringCol() > > class sqlmeta: > print 'class sqlmeta' > idName = 'title_id' [skip] > newKey = 1234 > newTitle = "Mystery Book" > book_b = titles.titles(title_id=newKey, title=newTitle) Throughout entire SQLObject the 'id' column is called 'id'. After you named your column 'title_id' in sqlmeta you have to use 'id' name for the column: book_b = titles.titles(id=newKey, title=newTitle) Without that SQLObject thinks id is None and creates a new id by asking the backend - see sqlobject/sybase/sybaseconnection.py, methods _queryInsertId() and insert_id(). insert_id() creates a new id (0) and then SQLObject tries to SELECT the row back by that id. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: <wzi...@co...> - 2009-03-04 16:14:04
|
Hello Oleg and others, But if I use 'id' instead of 'title_id' I get: File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1203, in __init__ File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1237, in _create TypeError: titles() did not get expected keyword argument 'title_id' I hope it helps if I use dots to preserve the indentation: ======================================= from.sqlobject.import.* class.titles(.SQLObject.): ....title_id = IntCol() ....title = StringCol() ....class.sqlmeta: ........print.'class.sqlmeta' ........idName = 'title_id' ........lazyUpdate = False ======================================= second class to insert a row: ======================================= from sqlobject import * import titles class InsertNewTitle: ....def makeNewBook( self ): ........sybURI = "sybase://my_user_id:my_password@DBSERVER//library_info" ........konnection = connectionForURI(sybURI) ........konnection.debug = True ........konnection.autoCommit = True ........sqlhub.processConnection = konnection ........newKey = 1237 ........newTitle = "Mystery Book" ........book_b = titles.titles(id=newKey, title=newTitle) ........print 'for ID %d the title is %s' % (book_b.id, book_b.title) ........return None if __name__ == "__main__": ....instantiatedObject = InsertNewTitle() ....instantiatedObject.makeNewBook() ========================================= I also tried mixing various combinations of 'id' and 'title_id' in the two locations but an error always results, with no indication what it really wants. Thanks very much, ....WZ |
From: Oleg B. <ph...@ph...> - 2009-03-04 16:23:09
|
On Wed, Mar 04, 2009 at 04:13:54PM +0000, wzi...@co... wrote: > Hello Oleg and others, > > But if I use 'id' instead of 'title_id' I get: > > File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1203, in __init__ > File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1237, in _create > TypeError: titles() did not get expected keyword argument 'title_id' Oops, my fault, sorry. You must not declare the 'id' column. > I hope it helps if I use dots to preserve the indentation: > ======================================= > from.sqlobject.import.* > > class.titles(.SQLObject.): > > ....title_id = IntCol() > ....title = StringCol() > > ....class.sqlmeta: > ........print.'class.sqlmeta' > ........idName = 'title_id' > ........lazyUpdate = False Remove 'title_id = IntCol()' from the class declaration. idName is the only way to name the column, and 'id' is the only way to refer to the column. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: <wzi...@co...> - 2009-03-04 16:51:23
|
No need to apologize; your help has been invaluable. It's working well now, so it's on to joins and other challenges! (Though I'll try not to bother you for a few days!) Thanks again, ....WZ ----- Original Message ----- From: "Oleg Broytmann" <ph...@ph...> To: sql...@li... Sent: Wednesday, March 4, 2009 4:22:59 PM GMT +00:00 Monrovia Subject: Re: [SQLObject] cannot insert with Sybase On Wed, Mar 04, 2009 at 04:13:54PM +0000, wzi...@co... wrote: > Hello Oleg and others, > > But if I use 'id' instead of 'title_id' I get: > > File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1203, in __init__ > File "/diska/data/workspace/PyXtern/Bibliothek/eggs/SQLObject-0.10.4-py2.5.egg/sqlobject/main.py", line 1237, in _create > TypeError: titles() did not get expected keyword argument 'title_id' Oops, my fault, sorry. You must not declare the 'id' column. > I hope it helps if I use dots to preserve the indentation: > ======================================= > from.sqlobject.import.* > > class.titles(.SQLObject.): > > ....title_id = IntCol() > ....title = StringCol() > > ....class.sqlmeta: > ........print.'class.sqlmeta' > ........idName = 'title_id' > ........lazyUpdate = False Remove 'title_id = IntCol()' from the class declaration. idName is the only way to name the column, and 'id' is the only way to refer to the column. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. ------------------------------------------------------------------------------ Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise -Strategies to boost innovation and cut costs with open source participation -Receive a $600 discount off the registration fee with the source code: SFAD http://p.sf.net/sfu/XcvMzF8H _______________________________________________ sqlobject-discuss mailing list sql...@li... https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss |
From: Oleg B. <ph...@ph...> - 2009-03-04 16:56:49
|
On Wed, Mar 04, 2009 at 04:51:17PM +0000, wzi...@co... wrote: > (Though I'll try not to bother you for a few days!) Actually, you can't. (-: Tomorrow I am leaving the town (even the country) for a short vacation. Will be back March 9, late at night. Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |