Guys,
I need help in figuring out some quirks in Elixir. I have a very simple schema (schema.py) with a simple ForeignKey relationship and a python module (model.py), where I create 2 databases from the same schema (see the code below). When I run model.py, I get the following error when it tries to create the second database:
Exception: 'Table2' resolves to several entities, you should use the full path (including the full module name) to that entity.
I am unsure why this error happens. It appears that the two database are sharing some state. What am I missing?
schema.py:import elixir
from elixir import Field, Unicode
from elixir import ManyToOne
class Table1(elixir.Entity):
# Disable the default global elixir.session.
elixir.using_options(tablename='Table1', session=None)
column1 = Field(Unicode, primary_key=True)
column2 = Field(Unicode, primary_key=True)
t2 = ManyToOne('Table2', field=column2,
ondelete='cascade', onupdate='cascade')
class Table2(elixir.Entity):
# Disable the default global elixir.session.
elixir.using_options(tablename='Table2', session=None)
column2 = Field(Unicode, primary_key=True)
model.py:
import sys
import elixir
import sqlalchemy, sqlalchemy.schema
from sqlalchemy.orm import scoped_session, sessionmaker
def import_schema(schema_module_string):
if schema_module_string in sys.modules:
#
# If the module is already imported, we reload it again.
# This is needed if we want to use same schema module for multiple
# databases, otherwise no tables are creates by setup_all()
#
reload(sys.modules[schema_module_string])
else:
# If the schema module is not imported, import it dynamically.
__import__(schema_module_string)
def create_model(database):
metadata = sqlalchemy.schema.ThreadLocalMetaData()
elixir.metadata = metadata
import_schema('schema')
elixir.setup_all()
engine = sqlalchemy.create_engine(database, echo='debug')
Session = scoped_session(sessionmaker(autoflush=True, bind=engine))
session = Session()
metadata.create_all(bind=engine)
session.commit()
def _main(argv=None):
create_model("sqlite:///foo")
# This is where the error happens
create_model("sqlite:///bar")
if __name__ == "__main__":
_main()