REPRO:
$ tg-admin sql drop
$ sqlite > drop table ... #drop each table if they are still there
$ tg-admin sql sql #see below
$ tg-admin sql create #see below
$ tg-admin shell (starts an ipython session)
1: p = Page(pagename="FooFoo")
4: dir(p)
'addColumn',
'addIndex',
'addJoin',
'byPagename',
ERROR:
- there should be an addEntry in the Page object 'p'
Note: this is a slightly different issue than the other recent
foreignkey post, as here, the tables are being created, but the foreign
key relation is not showing up, that I can see...
#model.py
class Page(SQLObject):
pagename = StringCol(alternateID=True, length=30)
entries = MultipleJoin('Entry')
class Entry(SQLObject):
data = StringCol()
page = ForeignKey('Page')
Page.createTable(ifNotExists=True)
Entry.createTable(ifNotExists=True)
#output from tg-admin sql sql:
Using database URI
sqlite:///home/tgreenwo/working/turbogear/toddswiki/wiki_db
CREATE TABLE entry (
id INTEGER PRIMARY KEY,
data TEXT,
page_id INT
);
CREATE TABLE page (
id INTEGER PRIMARY KEY,
pagename VARCHAR(30) NOT NULL UNIQUE
);
#output from sqlite > .dump
sqlite> .dump
BEGIN TRANSACTION;
CREATE TABLE page (
id INTEGER PRIMARY KEY,
pagename VARCHAR(30) NOT NULL UNIQUE
);
CREATE TABLE entry (
id INTEGER PRIMARY KEY,
data TEXT,
page_id INT
);
COMMIT;
Am I missing something here?
BTW - Yes, I started a new topic, as I have had no response on the
previous thread...
-Todd
#this is not correct, but shows what I'd like
Page.byPagename('FrontPage').entries(orderBy='revision')
#nor is this correct
Page.byPagename('FrontPage').entries.select(revision=1)
#From the docs, this looks to be the correct path:
for c in Entry.select(AND(Entry.pageID == Page.q.id,
Page.q.pagename=='FrontPage')):
print c
But this gives me:
ValueError: Unknown SQL builtin type: <type 'property'> for <property
object at 0x40842914>
#in a similar fashion...
list(Page.select())
<Page 1 pagename='FrontPage'>]
#again, this doesn't work as expected:
list (Entry.select(AND(Entry.pageID == Page.q.id,
Page.q.pagename=='FrontPage')))
ValueError: Unknown SQL builtin type: <type 'property'> for <property
object at 0x40842914>
Eventually, I suspect I'll use something like this to get the last
added wiki entry:
Entry.select(AND(Entry.pageID == Page.q.id,
Page.q.pagename=='FrontPage'), orderBy('revision)).reversed()[0:1]
So, how do I get there ?
-Todd
#model.py
class Page(SQLObject):
pagename = StringCol(alternateID=True, length=30)
entries = MultipleJoin('Entry')
class Entry(SQLObject):
data = StringCol()
revision = IntCol()
Hi Kevin,
some times ago I came across this:
http://tabo.aurealsys.com/software/xmms-pl-patch/?p=101
here you can find some interesting thoughts about django and
turbogears, and also about django ORM vs SQLObject, maybe you (and Ian)
can find something useful.
Ciao
Michele
p1 = Page(...)
p1.addEntry(Entry(...)) or p1.addEntry(...)
Not:
p1 = Page(...)
e = Entry(***page = p1***)
p1.addEntry(e)
Specifically, I'm suggesting that SQLObject introspect the added object
at this point, and make the nec primary key to foreign key mapping
automatically:
p1.addEntry(Entry(...)) #Internally, the Entry.page is set to p1 by
SQLObject, not the programmer
As an example of how Django does this sort of thing, here is a trimmed
down example from their web site:
The Django usage of this would be like so:
# Create a Reporter.
>>> r = reporters.Reporter(first_name='John')
<snip>
# Create an Article via the Reporter object.
>>> new_article = r.add_article(headline="John's second story")
http://www.djangoproject.com/documentation/models/many_to_one/
Model source code
from django.core import meta
class Reporter(meta.Model):
first_name = meta.CharField(maxlength=30)
class Article(meta.Model):
headline = meta.CharField(maxlength=100)
reporter = meta.ForeignKey(Reporter)
API reference
Reporter objects have the following methods:
* add_article()
* delete()
* get_article()
* get_article_count()
* get_article_list()
* save()
Article objects have the following methods:
* delete()
* get_reporter()
* save()
-Todd