sqlobject : inserting and retreiving data

0 views
Skip to first unread message

Todd Greenwood

unread,
Oct 25, 2005, 12:10:01 AM10/25/05
to TurboGears
How do I retreive / insert data using sqlobject? I have read the docs
front to back, upside down, then back again...and I am still stuck as
to how to actually use this thing beyond a super trivial example...

Here's my problem (this is an extension to the 20minuteWiki tutorial):

0.) The model:

class Page(SQLObject):
pagename = StringCol(alternateID=True, length=30)
attached_files = RelatedJoin('UploadedFile')
wikidata = MultipleJoin('WikiData')

class UploadedFile(SQLObject):
filename = StringCol(alternateID=True)
abspath = StringCol()
size = IntCol()
referenced_in_pages = RelatedJoin('Page')

class WikiData(SQLObject):
data = StringCol()
mod_date_time = DateTimeCol()
revision = IntCol()
author = StringCol(length=30)
referenced_in_pages = ForeignKey('Page')

Ob) tg-admin sql drop && tg-admin sql create

1.) How do I insert data into the 'Page', now that it is joined to 1toM
to WikiData?
- I have tried:
- p = Page(pagename='FrontPage')
- dir (p)... there is no addWikiData here...
'addColumn',
'addIndex',
'addJoin',
'addUploadedFile',


2.) How do I retrieve abitrary field data from the 'Page' table?
- assuming I can get data in, how do I get data from specific fields?
in this case, i only need to grab the last wiki entry, and get the
revision value from it...pseudocode:
p = Page.select(Page.q.pagename=='FrontPage'
orderBy(wikidata.revision)).reversed()[0:1]
rev = p.wikidata.revision

This last is making my hair turn grey. I have been able to get info by
doing the following (For this example there is no wikidata table, all
the fields belong to the Page table):

last_page_added = Page.select(Page.q.pagename==pagename,
orderBy=('revision')).reversed()[0:1]
for _page in last_page_added:
new_page = Page(pagename = pagename,
data = data,
last_mod_date_time = datetime.datetime.now(),
revision = _page.revision + 1
)

Isn't this a hack? Do I really have to iterate through a one item slice
in order to get the row values? Is there a way to more elegantly access
the data?

I would expect something like:

p = Page.select(Page.q.pagename==pagename,
orderBy=('revision')).reversed()[0:1]
print p.revision
print p.data
etc.

-Todd

Todd Greenwood

unread,
Oct 25, 2005, 2:50:22 AM10/25/05
to TurboGears
I've simplified the issue: I don't think that foreign keys are working
properly in sqlobject:

REPRO:
ipython session (tg-admin shell)
1: p = Page(pagename="FooFoo")
4: dir(p)


'addColumn',
'addIndex',
'addJoin',

#model.py


class Page(SQLObject):
pagename = StringCol(alternateID=True, length=30)

entries = MultipleJoin('Entry')

class Entry(SQLObject):
data = StringCol()
page = ForeignKey('Page')

ERROR:
- there should be an addEntry in the Page object 'p'

Seems like this is a similar issue here:
http://groups.google.com/group/turbogears/browse_frm/thread/5c0db984a1a17e74/1772a1b8a0ac21bc#1772a1b8a0ac21bc

Todd Greenwood

unread,
Oct 25, 2005, 3:18:07 PM10/25/05
to TurboGears
I've cross posted this issue to the SQLObject e-mail list
(sqlobjec...@lists.sourceforge.net) as follows:

I have a very simple model (below) that does not seem to process
properly due (I'm guessing here) to the presence of the Foreign Key:

REPRO:
$ tg-admin shell (starts an ipython session)


1: p = Page(pagename="FooFoo")
4: dir(p)
'addColumn',
'addIndex',
'addJoin',

ERROR:


- there should be an addEntry in the Page object 'p'

#model.py
class Page(SQLObject):
pagename = StringCol(alternateID=True, length=30)
entries = MultipleJoin('Entry')

class Entry(SQLObject):
data = StringCol()
page = ForeignKey('Page')

Am I missing something here?

Please cc me as I am not subscribed to this list.
-Todd

Reply all
Reply to author
Forward
0 new messages