Couldn't reply to the old topic.
The following setup works. However, I couldn't get nested packages in
database to work.
entities.py
----
#!/usr/bin/env python
# encoding: utf-8
from database import *
if __name__ == '__main__':
result = Norm.query.filter(Norm.year != None).all()
print result #Runs smoothly
database/__init__.py
----
#!/usr/bin/env python
# encoding: utf-8
from Norm import Norm
from NormType import NormType
from Parameter import Parameter
from ParameterGroup import ParameterGroup
from elixir import Entity, metadata, setup_all
def get_by_or_init(cls, if_new_set={}, **params):
"""Call get_by; if no object is returned, initialize an
object with the same parameters. If a new object was
created, set any initial values."""
result = cls.get_by(**params)
if not result:
result = cls(**params)
result.set(**if_new_set)
return result
Entity.get_by_or_init = classmethod(get_by_or_init)
def deffered(cls, deffered_class_name):
return cls._descriptor.collection.resolve(deffered_class_name,cls)
Entity.deffered = classmethod(deffered)
metadata.bind = "mysql://user:[EMAIL PROTECTED]/dbname?use_unicode=1"
metadata.bind.echo = False
metadata.bind.convert_unicode = True
setup_all()
database/join_tables.py
----
#!/usr/bin/env python
# encoding: utf-8
from sqlalchemy import Table, Column, Integer, ForeignKey
from elixir import metadata
grupo_de_parametros_parametro = Table('grupo_de_parametros_parametro',
metadata,
Column('grupo_de_parametros_id', Integer,
ForeignKey("grupos_de_parametros.id"), nullable=False,
primary_key=True),
Column('parametro_id', Integer, ForeignKey("parametros.id"),
nullable=False, primary_key=True),
)
database/Norm.py
----
#!/usr/bin/env python
# encoding: utf-8
from elixir import Entity, using_options, using_table_options,
ManyToOne, Field, Integer, String
from sqlalchemy.databases.mysql import MSYear
class Norm(Entity):
norm_type = ManyToOne('NormType', colname='tipo_de_norma_id')
number = Field(Integer(11), colname='numero')
parameter = ManyToOne('Parameter', colname='parametro_id')
year = Field(MSYear(), colname='ano')
using_options(tablename='normas')
def __repr__(self):
return '<Norm %s %s:%s>' % (
self.norm_type.name,
self.number,
self.year
)
database/NormType.py
----
#!/usr/bin/env python
# encoding: utf-8
from elixir import Entity, using_options, using_table_options, Field,
String
class NormType(Entity):
name = Field(String(255),colname='nome')
using_options(tablename='tipos_de_norma')
def __repr__(self):
return '<NormType %s>' % (self.name)
database/Parameter.py
----
#!/usr/bin/env python
# encoding: utf-8
from elixir import Entity, using_options, using_table_options,
ManyToMany, Field, String
from join_tables import grupo_de_parametros_parametro as gpp
class Parameter(Entity):
name = Field(String(255),colname='nome')
parameter_groups = ManyToMany('ParameterGroup',
table=gpp,
foreign_keys =lambda: [gpp.c.parametro_id,
gpp.c.grupo_de_parametros_id],
primaryjoin =lambda: Parameter.id == gpp.c.parametro_id,
secondaryjoin =lambda:
Parameter.deffered('ParameterGroup').id ==
gpp.c.grupo_de_parametros_id
)
using_options(tablename='parametros')
def __repr__(self):
return u'<Parameter "%s">' % (self.name)
database/ParameterGroup.py
----
#!/usr/bin/env python
# encoding: utf-8
from elixir import Entity, using_options, using_table_options,
ManyToMany, Field, String
from join_tables import grupo_de_parametros_parametro as gpp
class ParameterGroup(Entity):
name = Field(String(255),colname='nome')
parameters = ManyToMany('Parameter',
table=gpp,
foreign_keys =lambda: [gpp.c.parametro_id,
gpp.c.grupo_de_parametros_id],
primaryjoin =lambda: ParameterGroup.id ==
gpp.c.grupo_de_parametros_id,
secondaryjoin =lambda:
ParameterGroup.deffered('Parameter').id == gpp.c.parametro_id
)
using_options(tablename='grupos_de_parametros')
def __repr__(self):
return '<ParameterGroup "%s">' % (self.name)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---