[sqlalchemy] dynamic modification of foreign keys sqlalchemy

2010-12-01 Thread slothy
Hi!

I'm creating a structure of elements dynamically with declarative,
and I'm not sure if I can modify the mapper to get the new elements
related with a FK. Sorry about my english, is very poor.

This is my function class factory:

def nuevaEtiqueta(self, nombre_etiqueta):
etiqueta = nombre_etiqueta.lower().strip()
nombre_etiqueta = etiquetas_ + etiqueta
dicc = {'__tablename__':nombre_etiqueta,'__table_args__ ':
{'autoload':True}}

id = Column(Integer,primary_key=True);dicc.update({'id':id})
tipo = Column(Text);dicc.update({'tipo':tipo})
contenido_texto =
Column(Text);dicc.update({'contenido_texto':contenido_texto})
contenido_binario =
Column(LargeBinary);dicc.update({'contenido_binario':contenido_binario})
fichero = Column(Text);dicc.update({'fichero':fichero})
ref_elemento = Column(Integer,ForeignKey('elementos_'+etiqueta
+'.id'));dicc.update({'ref_elemento':ref_elemento})

def __init__(self, diccio):
  self.tipo = diccio[tipo]
  if self.tipo == BIN:
self.contenido_binario = diccio[contenido_binario]
self.fichero = diccio[fichero]
  else:
self.contenido_texto = diccio[contenido_texto]
dicc.update({'__init__':__init__})

def __repr__(self):
  aux = ETIQUETA 
  if self.tipo == 'BIN':
return aux +  BINARIA
  else:
return aux + %s - CONTENIDO: %s %
(self.tipo,self.contenido_texto)
dicc.update({'__repr__':__repr__})

clase_etiqueta = type(str(nombre_etiqueta),(Base,),dicc)
self.etiquetas_map.update({nombre_etiqueta:clase_etiqueta})

Now, when a new class is created, I want the mapper adds the FK

I'm trying to do this with session mappers but with no success.

Can you help?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] dynamic modification of foreign keys sqlalchemy

2010-12-01 Thread Michael Bayer

On Dec 1, 2010, at 5:40 AM, slothy wrote:

 Hi!
 
 I'm creating a structure of elements dynamically with declarative,
 and I'm not sure if I can modify the mapper to get the new elements
 related with a FK. Sorry about my english, is very poor.
 
 This is my function class factory:
 
 def nuevaEtiqueta(self, nombre_etiqueta):
etiqueta = nombre_etiqueta.lower().strip()
nombre_etiqueta = etiquetas_ + etiqueta
dicc = {'__tablename__':nombre_etiqueta,'__table_args__ ':
 {'autoload':True}}
 
id = Column(Integer,primary_key=True);dicc.update({'id':id})
tipo = Column(Text);dicc.update({'tipo':tipo})
contenido_texto =
 Column(Text);dicc.update({'contenido_texto':contenido_texto})
contenido_binario =
 Column(LargeBinary);dicc.update({'contenido_binario':contenido_binario})
fichero = Column(Text);dicc.update({'fichero':fichero})
ref_elemento = Column(Integer,ForeignKey('elementos_'+etiqueta
 +'.id'));dicc.update({'ref_elemento':ref_elemento})
 
def __init__(self, diccio):
  self.tipo = diccio[tipo]
  if self.tipo == BIN:
self.contenido_binario = diccio[contenido_binario]
self.fichero = diccio[fichero]
  else:
self.contenido_texto = diccio[contenido_texto]
dicc.update({'__init__':__init__})
 
def __repr__(self):
  aux = ETIQUETA 
  if self.tipo == 'BIN':
return aux +  BINARIA
  else:
return aux + %s - CONTENIDO: %s %
 (self.tipo,self.contenido_texto)
dicc.update({'__repr__':__repr__})
 
clase_etiqueta = type(str(nombre_etiqueta),(Base,),dicc)
self.etiquetas_map.update({nombre_etiqueta:clase_etiqueta})
 
 Now, when a new class is created, I want the mapper adds the FK
 
 I'm trying to do this with session mappers but with no success.

you can add a Foreign key to a Table using:

table.append_constraint(ForeignKeyConstraint(('local_col', ), 
('remote_table.remote_col', ))

you'd build functionality which does this at the same time as creating mappers. 
  





 
 Can you help?
 
 Thanks
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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 sqlalch...@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.