Marco Mariani ha scritto:
> Hi there
>
> This relates to Turbogears, but is really a SA question.
>
> I've customized TG authentication & authorization to use my autloaded
> tables in Postgres and SqlAlchemy 0.3.3.
>
> In my schema, I have User.c.uid, the login name of the users, as a
> primary key
>
> TG uses a User mapper with two distinct columns: User.c.user_id (the
> primary key) and User.c.user_name (the logname).
>
> Since I am an avid fan of meaningful primary keys (and have a legacy db
> to support) I want to keep things my way, but TG does some user handling
> that I have to fix.
>
> So, to avoid patches to the TG source or useless sub-classing, I'd like
> to access the same column by any of the three names.
>
> I cannot do that with a python property on the mapper because TG should
> be able to use get_by and friends.
>
>
> I've come up with:
>
>
> assign_mapper(context, User, tbl['users'], properties = {
>     'user_id': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
>     'user_name': tbl['users'].c.uid,   # alias for SqlAlchemyIdentity
>     'uid': tbl['users'].c.uid,
>     })
>
>
>
> This seems to work (I added the third property to make 'uid' reappear!)
> , but makes it impossible, for instance, to create new users:
>
> In [1]: user = User(uid='xxx')
>
> In [2]: session.flush()
> [...]
> SQLError: (IntegrityError) null value in column "uid" violates not-null
> constraint
>  'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
> (%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None, 'name':
> None}
>
> In [3]: user = User(user_id='xxx')
>
> In [4]: session.flush()
> [...]
> SQLError: (IntegrityError) null value in column "uid" violates not-null
> constraint
>  'INSERT INTO users (uid, nome, cognome, codice_fiscale) VALUES
> (%(uid)s, %(name)s, %(surname)s)' {'surname': None, 'uid': None, 'name':
> None}
>
>
>
> I reckon I should probably go ahead and patch TG, but maybe there is a
> clean way to do what I have in mind?
>
> Thank you.
>
>
> >
>   
Hi Marco!
I mapped User in my model something similar to  yours.
I have no an 'email_address' column  neither a 'display_name' and I 
access those fields by a lookup in an external table.
I have no, an 'user_name' but a 'logname' column and I have an 'id' 
instead of an 'user_id' column and it works for me. I am able to insert 
new rows with no problems.

Here my definition:

class User(DomainObject):
    @property
    def permissions(self):
        perms = set()
        for g in self.groups:
            perms = perms | set(g.permissions)
        return perms
    @property
    def email_address(self):
        return self.anagrafica.email
    @property
    def display_name(self):
        return self.anagrafica.nome

assign_mapper(context, User, tbl['operatore'],
    properties = {
                'anagrafica' : relation(Anagrafica,
                               primaryjoin = tbl['anagrafica'].c.id == 
tbl['operatore'].c.id_anagrafica,
                                                           backref = 
'anagrafiche'),
                'user_id'     : tbl['operatore'].c.id,
                'user_name'   : tbl['operatore'].c.logname,
                'id'          : tbl['operatore'].c.id,
                'logname'     : tbl['operatore'].c.logname,
                }
            )
               

jo


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to