From: Cedric B. <wo...@in...> - 2005-07-28 14:26:44
|
hi, I'm trying to have a one-many relations between Person and Email. But I'd like to work easily with person to set a PRIMARY email. So I've prepared (just for you :) ) an example to test out of the box: ================================================================== from sqlobject import * __connection__ = 'postgres://briner:@idas/test' def set_isprimary(self, value): ''' in relation with the email class: this function allow to have only and only one primary email between all the emails. If we want an email to be the primary one for the person foo, it will setup that email has primary and set to ``not primary'' the former primary one''' if 0==value: self._SO_set_isprimary(value) else: query=self.__class__.select(# AND( self.__class__.q.personID== self.personID # ,self.__class__.q.isprimary==1 # ) ) assert 2>len(list(query)) # only one primary email by person ! if 0==len(list(query)): self._SO_set_isprimary(value) else: try: self.id # has_id is a way to know if the class already exist # (already in the sql database) # or if it's being created (__init__) has_id=True except: has_id=False if has_id: if not(query[0] is self): query[0].isprimary=0 self._SO_set_isprimary(value) else: query[0].isprimary=0 self._SO_set_isprimary(value) class Person(SQLObject): guarantor= ForeignKey('Person', default=None) lastname= UnicodeCol(length=32, unique=True, notNone=True) firstname= UnicodeCol(length=32, unique=True, notNone=True) emails=MultipleJoin('Email') def _get_primary_email(self): query=Email.select( AND( Email.q.isprimary==1 ,Email.q.personID==self.id ) ) assert 2>len(list(query)), 'only one primary key can occur' if 1 == len(list(query)): return query[0] else: return None def _set_primary_email(self, email): emails=Email.select( AND( Email.q.email==email ,Email.q.personID==self.id ) ) assert 2>len(list(emails)), 'only one primary key can occur' if 1 == len(list(emails)): emails[0].isprimary=1 return Email(email=email, isprimary=1, person=self.id) return class Email(SQLObject): person=ForeignKey('Person', notNone=True) email=StringCol(length=128, notNone=True) type=StringCol(length=64, default=None) isprimary=IntCol(notNone=True, default=0) _set_isprimary=set_isprimary Email.dropTable(ifExists=True) Person.dropTable(ifExists=True) Person.createTable() Email.createTable() p=Person(lastname='briner', firstname='cedric') Email(email='briner@', person=p) Email(email='work@', person=p) p.primary_email='work@' print p.primary_email print p.emails p.primary_email='work@' print p.primary_email print p.emails p.primary_email='briner@' print p.primary_email print p.emails print 'it just not work as expected here !' p.primary_email='cedric.briner@' ## pure incomprehension to me print p.primary_email print p.emails ============================================= thanks in advance for your time Ced. -- Cedric BRINER Geneva - Switzerland |
From: michelts <mic...@gm...> - 2005-07-28 14:38:01
|
Hi, There is another way to do this task I like, reather than setting an attribute in the email defining the primary email, I create a foreign key like this: class Person(SQLObject): name =3D StringCol() primaryEmail =3D ForeignKey('Email', default=3DNone) emails =3D MultipleJoin('Email') class Email(SQLObject): address =3D StringCol() person =3D ForeignKey('Person') p=3DPerson(name=3D'Michel Thadeu Sabchuk') e1=3DEmail(adrress=3D'mic...@gm...', person=3Dp) e2=3DEmail(address=3D'mic...@ya...', person=3Dp) e3=3DEmail(address=3D'mi...@br...', person=3Dp) p.primaryEmail =3D e1 regards, On 7/28/05, Cedric BRINER <wo...@in...> wrote: > hi, >=20 > I'm trying to have a one-many relations between Person and Email. But I'd= like to work easily with person to set a PRIMARY email. >=20 > So I've prepared (just for you :) ) an example to test out of the box: >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > from sqlobject import * > __connection__ =3D 'postgres://briner:@idas/test' >=20 >=20 > def set_isprimary(self, value): > ''' in relation with the email class: > this function allow to have only and only one primary email between > all the emails. If we want an email to be the primary one for the per= son > foo, it will setup that email has primary and set to ``not primary'' > the former primary one''' > if 0=3D=3Dvalue: > self._SO_set_isprimary(value) > else: > query=3Dself.__class__.select(# > AND( > self.__class__.q.personID=3D=3D self.personID # > ,self.__class__.q.isprimary=3D=3D1 # > ) > ) > assert 2>len(list(query)) # only one primary email by person ! > if 0=3D=3Dlen(list(query)): > self._SO_set_isprimary(value) > else: > try: > self.id > # has_id is a way to know if the class already exist > # (already in the sql database) > # or if it's being created (__init__) > has_id=3DTrue > except: > has_id=3DFalse > if has_id: > if not(query[0] is self): > query[0].isprimary=3D0 > self._SO_set_isprimary(value) > else: > query[0].isprimary=3D0 > self._SO_set_isprimary(value) >=20 >=20 > class Person(SQLObject): > guarantor=3D ForeignKey('Person', default=3DNone) > lastname=3D UnicodeCol(length=3D32, unique=3DTrue, notNone=3DTrue) > firstname=3D UnicodeCol(length=3D32, unique=3DTrue, notNone=3DTrue) > emails=3DMultipleJoin('Email') > def _get_primary_email(self): > query=3DEmail.select( > AND( > Email.q.isprimary=3D=3D1 > ,Email.q.personID=3D=3Dself.id > ) > ) > assert 2>len(list(query)), 'only one primary key can occur' > if 1 =3D=3D len(list(query)): > return query[0] > else: > return None > def _set_primary_email(self, email): > emails=3DEmail.select( > AND( > Email.q.email=3D=3Demail > ,Email.q.personID=3D=3Dself.id > ) > ) > assert 2>len(list(emails)), 'only one primary key can occur' > if 1 =3D=3D len(list(emails)): > emails[0].isprimary=3D1 > return > Email(email=3Demail, isprimary=3D1, person=3Dself.id) > return >=20 > class Email(SQLObject): > person=3DForeignKey('Person', notNone=3DTrue) > email=3DStringCol(length=3D128, notNone=3DTrue) > type=3DStringCol(length=3D64, default=3DNone) > isprimary=3DIntCol(notNone=3DTrue, default=3D0) > _set_isprimary=3Dset_isprimary >=20 >=20 > Email.dropTable(ifExists=3DTrue) > Person.dropTable(ifExists=3DTrue) > Person.createTable() > Email.createTable() > p=3DPerson(lastname=3D'briner', firstname=3D'cedric') > Email(email=3D'briner@', person=3Dp) > Email(email=3D'work@', person=3Dp) >=20 >=20 > p.primary_email=3D'work@' > print p.primary_email > print p.emails >=20 >=20 > p.primary_email=3D'work@' > print p.primary_email > print p.emails >=20 > p.primary_email=3D'briner@' > print p.primary_email > print p.emails >=20 > print 'it just not work as expected here !' > p.primary_email=3D'cedric.briner@' ## pure incomprehension to me > print p.primary_email > print p.emails >=20 >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > thanks in advance for your time >=20 > Ced. > -- >=20 > Cedric BRINER > Geneva - Switzerland >=20 >=20 > ------------------------------------------------------- > SF.Net email is Sponsored by the Better Software Conference & EXPO Septem= ber > 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices > Agile & Plan-Driven Development * Managing Projects & Teams * Testing & Q= A > Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss >=20 --=20 Michel Thadeu Sabchuk Curitiba - Brasil |
From: Cedric B. <wo...@in...> - 2005-07-28 15:29:06
|
> class Person(SQLObject): > name = StringCol() > primaryEmail = ForeignKey('Email', default=None) > emails = MultipleJoin('Email') > > class Email(SQLObject): > address = StringCol() > person = ForeignKey('Person') > > p=Person(name='Michel Thadeu Sabchuk') > e1=Email(adrress='mic...@gm...', person=p) > e2=Email(address='mic...@ya...', person=p) > e3=Email(address='mi...@br...', person=p) > p.primaryEmail = e1 > > regards, but in that case how do create the database. If I'm trying to create Person it is complaining about Email and vice-versa. Is there a way to create firstly the table itself, without the foreign_key, and then alter the tables to add the foreign key column ? How do you proceed ? Ced. P.-S. I didn't even appear to me that from the documentation that we were able to have an one-one +relation like the way you explained ! -- Cedric BRINER Geneva - Switzerland |