On Thu, Dec 30, 2010 at 12:26:50PM -0800, Simon Laalo wrote:
> I'm fairly new to SQLObject
Welcome!
> I have a question about RelatedJoin: is it possible to use an alternateID as the data for the otherColumn.
>
> I have something like the following
>
> in the DB:
> users table with columns id ( int) and username (char 8)
>
> roles table with columns id (int) and name (char 8)
>
> user_roles table with columns username and role_id
>
> in the model.py:
> class User(SQLObject):
> class sqlmeta:
> table = 'users'
> username = UnicodeCol(alternateID=True, varchar=False, length=8,alternateMethodName='by_username')
>
>
> class Role(SQLObject):
> class sqlmeta:
> table = 'roles'
> name = UnicodeCol(varchar=False, length=8)
>
> users = RelatedJoin('User', intermediateTable='user_roles',
> joinColumn='role_id', otherColumn='username')
>
> but this isn't working because when I try to get role.users it attempts to find users whose ID is their username.
>
> Is there a way for this to work using the user's alternateID username in the join table?
First, why do you want this at all? Why not allow SQLObject to do
internal referencing itself?
Well, if you still want to do the work manually, well... it has to be
performed manually.
RelatedJoin is many-to-many relation (BTW, do you need many-to-many
or one-to-many which is implemented by MultipleJoin) and works using an
intermediate table that stores references to both tables. By default the
intermediate table is created with INT columns to store references to
id's. If you want to store references to a UnicodeCol you have to create
the intermediate table yourself:
http://sqlobject.org/FAQ.html#how-can-i-define-my-own-intermediate-table-in-my-many-to-many-relationship
And joinColumn has to be "username" in this case.
I've never tried doing something like this, so I'm not sure it will
work.
Oleg.
--
Oleg Broytman http://phdru.name/ ph...@ph...
Programmers don't die, they just GOSUB without RETURN.
|