1
Notes on using SQLObject
2
========================
7
We inherit from sqlos.SQLOS rather than sqlobject.SQLObject, to get the Zope 3
8
glue that sqlos provides, e.g. per-thread connections.
13
SQLObject's default naming "style" for translating Python names (such as
14
class and attribute names) to SQL names (such as table and column names) doesn't
15
match our SQL's naming scheme. As a result, we can't simply say::
17
class SourcePackageRelease(SQLOS):
18
"""A source package release."""
22
Instead, we need to say::
24
class SourcePackageRelease(SQLOS):
25
"""A source package release."""
27
_table = 'SourcePackageRelease'
30
TODO: This should be fixable by defining our own style (see the sqlobject.styles
33
Foreign Keys and Joins
34
----------------------
36
SQLObject also tries to guess names for foreign keys, but doesn't provide anyway
37
to hook that with its styles mechanism, so again we need to explicitly tell
38
SQLObject the names to use. See this example::
41
"""A specific branch in Arch (archive/category--branch--version)"""
47
description = StringCol(dbName='description')
48
changesets = MultipleJoin('Changeset', joinColumn='branch')
51
class Changeset(SQLOS):
54
implements(IChangeset)
58
branch = ForeignKey(foreignKey='Branch', dbName='branch',
60
message = StringCol(dbName='logmessage', notNull=True)
62
Note the passing of `name`, `foreignKey` and `dbName` to the ForeignKey column.
64
This example also demonstrates the MultipleJoin feature of SQLObject, which is
65
used for one-to-many relationships. e.g.::
67
Grab a random branch out of the DB
68
>>> branch = Branch.select()[0]
70
Grab that branch's changesets
71
>>> changesets = branch.changesets
73
A changeset's branch attribute gives us the original branch
74
>>> changesets[0].branch is branch
77
The string you pass to MultipleJoin is the name of another SQLOS subclass, as is
78
string passed as the `foreignKey` argument to ForeignKey (strings are used so
79
that you can reference a class that hasn't been declared yet). We also need to
80
specify the `joinColumn` (SQLObject guesses the wrong name for it, like