[sqlalchemy] How to write and access attribute in many to many table

2011-03-14 Thread Christian Démolis
Hi all,

I have a question about many to many Table containing attribute.
How to access and write Max attribute in many to many table ?

I already read that but i try to not use mapper and stay in declarative mode
which is more user friendly :)
http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html

PlageTypeActe = Table('plage_type_acte', Base.metadata,
Column('IdPlage', Integer, ForeignKey('plage_type.IdPlage'),
primary_key=True),
Column('IdActe', Integer, ForeignKey('acte.IdActe'), primary_key=True),
*Column('Max', Integer)*
)

class PlageType(Base):
__tablename__ = 'plage_type'
Typage = 2
IdPlage = Column('IdPlage', Integer, primary_key=True)
Debut = Column('Debut', DateTime)
Fin = Column('Fin', DateTime)
JourSemaine = Column('JourSemaine', Integer)
EtatPrecedent = Column('EtatPrecedent', String)
EtatCourant = Column('EtatCourant', String)
EtatSuivant = Column('EtatSuivant', String)
Max = Column('Max', Integer)
SurchargeVad = Column('SurchargeVad', Boolean)
StopVad = Column('StopVad', Time)
Creation = Column('Creation', DateTime)
Modification = Column('Modification', DateTime)
MaxDejaVenu = Column('MaxDejaVenu', Integer)
MaxJamaisVenu = Column('MaxJamaisVenu', Integer)
MaxActif = Column('MaxActif', Integer)
MaxInactif = Column('MaxInactif', Integer)
MaxPatient = Column('MaxPatient', Integer)
MaxDelegue = Column('MaxDelegue', Integer)
MaxCmu = Column('MaxCmu', Integer)
MaxNonCmu = Column('MaxNonCmu', Integer)
AgeMini = Column('AgeMini', Integer)
AgeMaxi = Column('AgeMaxi', Integer)
IdSemaineType = Column('IdSemaineType',
ForeignKey('semaine_type.IdSemaineType'))
IdDossier = Column('IdDossier', ForeignKey('dossier.IdDossier'))
IdLieu = Column('IdLieu', ForeignKey('lieu.IdLieu'))
NomRemplacement = Column('NomRemplacement', String)
*PlageTypeActe = relationship(Acte, secondary=PlageTypeActe,
backref=plage_type)*
def __init__(self,
Debut=datetime.datetime.today(),
Fin=datetime.datetime.today(),
JourSemaine=0,
EtatPrecedent=,
EtatCourant=,
EtatSuivant=,
Max=0,
SurchargeVad=0,
StopVad=datetime.time(),
Creation=datetime.datetime.today(),
Modification=datetime.datetime.today(),
MaxDejaVenu=-1,
MaxJamaisVenu=-1,
MaxActif=-1,
MaxInactif=-1,
MaxPatient=-1,
MaxDelegue=-1,
MaxCmu=-1,
MaxNonCmu=-1,
AgeMini = 0,
AgeMaxi = 0,
IdSemaineType = 0,
IdDossier = 0,
IdLieu = 0,
NomRemplacement = 
):
self.Debut = Debut
self.Fin = Fin
self.JourSemaine = JourSemaine
self.EtatPrecedent = EtatPrecedent
self.EtatCourant = EtatCourant
self.EtatSuivant = EtatSuivant
self.Max = Max
self.SurchargeVad = SurchargeVad
self.StopVad = StopVad
self.Creation = Creation
self.Modification = Modification
self.MaxDejaVenu = MaxDejaVenu
self.MaxJamaisVenu = MaxJamaisVenu
self.MaxActif = MaxActif
self.MaxInactif = MaxInactif
self.MaxPatient = MaxPatient
self.MaxDelegue = MaxDelegue
self.MaxCmu = MaxCmu
self.MaxNonCmu = MaxNonCmu
self.AgeMini = AgeMini
self.AgeMaxi = AgeMaxi
self.IdSemaineType = IdSemaineType
self.IdDossier = IdDossier
self.IdLieu = IdLieu
self.NomRemplacement = NomRemplacement

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: How to write and access attribute in many to many table

2011-03-14 Thread Eric Ongerth
Nothing prevents the use of associationproxy with Declarative.


On Mar 14, 3:26 am, Christian Démolis christiandemo...@gmail.com
wrote:
 Hi all,

 I have a question about many to many Table containing attribute.
 How to access and write Max attribute in many to many table ?

 I already read that but i try to not use mapper and stay in declarative mode
 which is more user friendly 
 :)http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html

 PlageTypeActe = Table('plage_type_acte', Base.metadata,
     Column('IdPlage', Integer, ForeignKey('plage_type.IdPlage'),
 primary_key=True),
     Column('IdActe', Integer, ForeignKey('acte.IdActe'), primary_key=True),
 *    Column('Max', Integer)*
 )

 class PlageType(Base):
     __tablename__ = 'plage_type'
     Typage = 2
     IdPlage = Column('IdPlage', Integer, primary_key=True)
     Debut = Column('Debut', DateTime)
     Fin = Column('Fin', DateTime)
     JourSemaine = Column('JourSemaine', Integer)
     EtatPrecedent = Column('EtatPrecedent', String)
     EtatCourant = Column('EtatCourant', String)
     EtatSuivant = Column('EtatSuivant', String)
     Max = Column('Max', Integer)
     SurchargeVad = Column('SurchargeVad', Boolean)
     StopVad = Column('StopVad', Time)
     Creation = Column('Creation', DateTime)
     Modification = Column('Modification', DateTime)
     MaxDejaVenu = Column('MaxDejaVenu', Integer)
     MaxJamaisVenu = Column('MaxJamaisVenu', Integer)
     MaxActif = Column('MaxActif', Integer)
     MaxInactif = Column('MaxInactif', Integer)
     MaxPatient = Column('MaxPatient', Integer)
     MaxDelegue = Column('MaxDelegue', Integer)
     MaxCmu = Column('MaxCmu', Integer)
     MaxNonCmu = Column('MaxNonCmu', Integer)
     AgeMini = Column('AgeMini', Integer)
     AgeMaxi = Column('AgeMaxi', Integer)
     IdSemaineType = Column('IdSemaineType',
 ForeignKey('semaine_type.IdSemaineType'))
     IdDossier = Column('IdDossier', ForeignKey('dossier.IdDossier'))
     IdLieu = Column('IdLieu', ForeignKey('lieu.IdLieu'))
     NomRemplacement = Column('NomRemplacement', String)
     *PlageTypeActe = relationship(Acte, secondary=PlageTypeActe,
 backref=plage_type)*
     def __init__(self,
         Debut=datetime.datetime.today(),
         Fin=datetime.datetime.today(),
         JourSemaine=0,
         EtatPrecedent=,
         EtatCourant=,
         EtatSuivant=,
         Max=0,
         SurchargeVad=0,
         StopVad=datetime.time(),
         Creation=datetime.datetime.today(),
         Modification=datetime.datetime.today(),
         MaxDejaVenu=-1,
         MaxJamaisVenu=-1,
         MaxActif=-1,
         MaxInactif=-1,
         MaxPatient=-1,
         MaxDelegue=-1,
         MaxCmu=-1,
         MaxNonCmu=-1,
         AgeMini = 0,
         AgeMaxi = 0,
         IdSemaineType = 0,
         IdDossier = 0,
         IdLieu = 0,
         NomRemplacement = 
         ):
         self.Debut = Debut
         self.Fin = Fin
         self.JourSemaine = JourSemaine
         self.EtatPrecedent = EtatPrecedent
         self.EtatCourant = EtatCourant
         self.EtatSuivant = EtatSuivant
         self.Max = Max
         self.SurchargeVad = SurchargeVad
         self.StopVad = StopVad
         self.Creation = Creation
         self.Modification = Modification
         self.MaxDejaVenu = MaxDejaVenu
         self.MaxJamaisVenu = MaxJamaisVenu
         self.MaxActif = MaxActif
         self.MaxInactif = MaxInactif
         self.MaxPatient = MaxPatient
         self.MaxDelegue = MaxDelegue
         self.MaxCmu = MaxCmu
         self.MaxNonCmu = MaxNonCmu
         self.AgeMini = AgeMini
         self.AgeMaxi = AgeMaxi
         self.IdSemaineType = IdSemaineType
         self.IdDossier = IdDossier
         self.IdLieu = IdLieu
         self.NomRemplacement = NomRemplacement

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] How to write and access attribute in many to many table

2011-03-14 Thread Michael Bayer

On Mar 14, 2011, at 6:26 AM, Christian Démolis wrote:

 Hi all,
 
 I have a question about many to many Table containing attribute.
 How to access and write Max attribute in many to many table ?
 
 I already read that but i try to not use mapper and stay in declarative mode 
 which is more user friendly :)
 http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html

not sure why google keeps returning the 05 docs, I've got them disallowed in 
robots.txt.

Here's the current docs which illustrate a declarative example:

http://www.sqlalchemy.org/docs/orm/relationships.html#association-object




 
 PlageTypeActe = Table('plage_type_acte', Base.metadata,
 Column('IdPlage', Integer, ForeignKey('plage_type.IdPlage'), 
 primary_key=True),
 Column('IdActe', Integer, ForeignKey('acte.IdActe'), primary_key=True),
 Column('Max', Integer)
 )
 
 class PlageType(Base):
 __tablename__ = 'plage_type'
 Typage = 2
 IdPlage = Column('IdPlage', Integer, primary_key=True)
 Debut = Column('Debut', DateTime)
 Fin = Column('Fin', DateTime)
 JourSemaine = Column('JourSemaine', Integer)
 EtatPrecedent = Column('EtatPrecedent', String)
 EtatCourant = Column('EtatCourant', String)
 EtatSuivant = Column('EtatSuivant', String)
 Max = Column('Max', Integer)
 SurchargeVad = Column('SurchargeVad', Boolean)
 StopVad = Column('StopVad', Time)
 Creation = Column('Creation', DateTime)
 Modification = Column('Modification', DateTime)
 MaxDejaVenu = Column('MaxDejaVenu', Integer)
 MaxJamaisVenu = Column('MaxJamaisVenu', Integer)
 MaxActif = Column('MaxActif', Integer)
 MaxInactif = Column('MaxInactif', Integer)
 MaxPatient = Column('MaxPatient', Integer)
 MaxDelegue = Column('MaxDelegue', Integer)
 MaxCmu = Column('MaxCmu', Integer)
 MaxNonCmu = Column('MaxNonCmu', Integer)
 AgeMini = Column('AgeMini', Integer)
 AgeMaxi = Column('AgeMaxi', Integer)
 IdSemaineType = Column('IdSemaineType', 
 ForeignKey('semaine_type.IdSemaineType'))
 IdDossier = Column('IdDossier', ForeignKey('dossier.IdDossier'))
 IdLieu = Column('IdLieu', ForeignKey('lieu.IdLieu'))
 NomRemplacement = Column('NomRemplacement', String)
 PlageTypeActe = relationship(Acte, secondary=PlageTypeActe, 
 backref=plage_type)
 def __init__(self,
 Debut=datetime.datetime.today(),
 Fin=datetime.datetime.today(),
 JourSemaine=0,
 EtatPrecedent=,
 EtatCourant=,
 EtatSuivant=,
 Max=0,
 SurchargeVad=0,
 StopVad=datetime.time(),
 Creation=datetime.datetime.today(),
 Modification=datetime.datetime.today(),
 MaxDejaVenu=-1,
 MaxJamaisVenu=-1,
 MaxActif=-1,
 MaxInactif=-1,
 MaxPatient=-1,
 MaxDelegue=-1,
 MaxCmu=-1,
 MaxNonCmu=-1,
 AgeMini = 0,
 AgeMaxi = 0, 
 IdSemaineType = 0,
 IdDossier = 0,
 IdLieu = 0,
 NomRemplacement = 
 ):
 self.Debut = Debut
 self.Fin = Fin
 self.JourSemaine = JourSemaine
 self.EtatPrecedent = EtatPrecedent
 self.EtatCourant = EtatCourant
 self.EtatSuivant = EtatSuivant
 self.Max = Max
 self.SurchargeVad = SurchargeVad
 self.StopVad = StopVad
 self.Creation = Creation
 self.Modification = Modification
 self.MaxDejaVenu = MaxDejaVenu
 self.MaxJamaisVenu = MaxJamaisVenu
 self.MaxActif = MaxActif
 self.MaxInactif = MaxInactif
 self.MaxPatient = MaxPatient
 self.MaxDelegue = MaxDelegue
 self.MaxCmu = MaxCmu
 self.MaxNonCmu = MaxNonCmu
 self.AgeMini = AgeMini
 self.AgeMaxi = AgeMaxi
 self.IdSemaineType = IdSemaineType
 self.IdDossier = IdDossier
 self.IdLieu = IdLieu
 self.NomRemplacement = NomRemplacement
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: InvalidRequestError

2011-03-14 Thread Ajay
Thanks Mike for your quick response.

I removed the zope.sqlalchemy extension and I am no longer getting the
zope error.

Since we are using a scoped_session, we don't think it's a threading
issue although we are running in a multi-threaded env.

I am trying to create this exception to occur in my test env.

What state do we have to leave the Session object in - inorder to
generate an 'InvalidRequestError' exception.
Again, assuming that it is not a multithreaded concurrency issue given
I am using sessions produced from ScopedSession.

My assumption is that since I am using ScopedSession, each thread gets
it's own session object and session objects are not shared among
threads.

Again appreciate your help with this.

thanks!

Ajay




On Mar 12, 11:38 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Mar 11, 2011, at 7:51 PM, AJAY PATTNI wrote:



  We use sqlalchemy 0.4.6 with Elixir 0.5.2 and zope.sqlalchemy

  Every once a while we get this error(see below):
  In a previous thread somebody said, just 'rollback your session when
  the exception is raised'?

  We use a global session as follows:

  __session__ =
  scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True,extension=ZopeTransactionExtension()))

  All the rest of the transactional machinery is handled by Elixiry
  Entity object.

  So my question - how can we rollback in this situation?

  We tried to do __session__.remove() which seems to work but then zope
  seems to have a problem after it completes the request.

 A description of what this error means from the perspective of SQLAlchemy 
 only, not that of zope.sqlalchemy for which you should consult their mailing 
 list, is here:

        
 http://www.sqlalchemy.org/trac/wiki/FAQ#Thetransactionisinactivedueto...

 However, note that version 0.4.6 of SQLAlchemy is extremely old, and the 
 mechanics of session and transaction have been largely reworked since then.   
 The basic idea that a rollback needs to occur is consistent in that version, 
 however.

 Its very likely that some artifact of zope.sqlalchemy is involved in the 
 production of your error here.

  ==
  2011-03-11T10:41:22 ERROR Zope.SiteErrorLog
 http://dragon.initiatesystems.com:8080/ati/txm_inject_orm
  Traceback (innermost last):
   Module ZPublisher.Publish, line 121, in publish
   Module Zope2.App.startup, line 238, in commit
   Module transaction._manager, line 89, in commit
   Module transaction._transaction, line 329, in commit
   Module transaction._transaction, line 446, in _commitResources
   Module zope.sqlalchemy.datamanager, line 76, in tpc_vote
   Module sqlalchemy.orm.session, line 263, in commit
   Module sqlalchemy.orm.session, line 176, in _assert_is_open
  InvalidRequestError: The transaction is closed
  =

  Appreciate any help I can get on this.

  
  Mar 4 12:56:23 pbrk4.den05.accenx.com [err]
 https://acm.accenx.com/mapsrvcluster/ati/acm/tdm/group/19150/broker/2...
  (innermost last):#012 Module ZPublisher.Publish, line 115, in
  publish#012 Module ZPublisher.mapply, line 88, in mapply#012 Module
  ZPublisher.Publish, line 41, in call_object#012 Module
  Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
  Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
  Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
  None, line 90, in processMsg#012 - PythonScript at /ati/acm/tdm/group/
  processMsg used for /ati/acm/tdm/group/19150/broker/25150/site/
  705077435#012 - Line 90#012 Module Shared.DC.Scripts.Bindings, line
  313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
  _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
  in _exec#012 Module None, line 131, in translate#012 - PythonScript
  at /ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
  translate#012 - Line 131#012 Module Shared.DC.Scripts.Bindings, line
  313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
  _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
  in _exec#012 Module None, line 99, in callTxm#012 - PythonScript at /
  ati/acm/tdm/group/19150/broker/25150/site/705077435/outbound/
  callTxm#012 - Line 99#012 Module Shared.DC.Scripts.Bindings, line
  313, in __call__#012 Module Shared.DC.Scripts.Bindings, line 350, in
  _bindAndExec#012 Module Products.PythonScripts.PythonScript, line 326,
  in _exec#012 Module None, line 44, in txm_ampiWrapperOrm#012 -
  PythonScript at /ati/acm/tdm/group/19150/broker/25150/
  BrokerMapFunctions/TxmFunctions/txm_ampiWrapperOrm#012 - Line 44#012
  Module Shared.DC.Scripts.Bindings, line 313, in __call__#012 Module
  Shared.DC.Scripts.Bindings, line 350, in _bindAndExec#012 Module
  Products.PythonScripts.PythonScript, line 326, in _exec#012 Module
  None, line 22, in getTransactionTypes#012 - PythonScript at /ati/acm/
  tdm/group/19150/broker/25150/BrokerMapFunctions/TxmFunctions/
  

[sqlalchemy] two-way attribute relationships through associationproxy

2011-03-14 Thread Eric Ongerth

from sqlalchemy import Unicode, Integer, Column, create_engine,
ForeignKey
from sqlalchemy.orm import relationship, Session
from sqlalchemy.orm.collections import MappedCollection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
import operator

class Base(object):
id = Column(Integer, primary_key=True)

Base = declarative_base(cls=Base)

def _create_c_by_value(value):
return C(value)

def _create_a_by_value(value):
return A(value)

class A(Base):
__tablename__ = a
id = Column(Integer, primary_key=True)
value = Column(Unicode)
associations = relationship(B, cascade=all)
c_values = association_proxy(associations, c_val,
creator=_create_c_by_value)

def __init__(self, val):
self.value = val

def __repr__(self):
return('A(%s)' % self.value)

class B(Base):
__tablename__ = b
a_id = Column(Integer, ForeignKey(a.id), nullable=False)
c_id = Column(Integer, ForeignKey(c.id), nullable=False)
c_elements = relationship(C, cascade=all)
c_val = association_proxy(c_elements, value)
a_elements = relationship(A, cascade=all)
a_val = association_proxy(a_elements, value)


class C(Base):
__tablename__ = c
id = Column(Integer, primary_key=True)
c_value = Column(Unicode)
associations = relationship(B, cascade=all)
a_values = association_proxy(associations, a_val,
creator=_create_a_by_value)

def __init__(self, val):
self.value = val

def __repr__(self):
return('C(%s)' % self.value)


if __name__ == __main__:

engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)
session = Session(engine)



Ok, running this sets up an example environment then you can
experiment.  Try creating some As and relating them to some Cs.  I'm
getting a bunch of different errors doing this.  What am I doing wrong
or overlooking?

The core of my inquiry is this: would this be the way to make two-way
relationships between two classes (two classes which have a many:many
relationship between them) work correctly when tunneled through an
associationproxy to hide the association object class between them?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.