Create multiple database from same schema

36 views
Skip to first unread message

Gaurav Gupta

unread,
May 2, 2012, 12:38:06 PM5/2/12
to sqle...@googlegroups.com
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() 

Gaurav Gupta

unread,
May 2, 2012, 10:33:24 PM5/2/12
to sqle...@googlegroups.com
Guys, I can really use some help here. Can some one tell me why I am unable to do such a simple thing.

Thanks,
Gaurav

yoav glazner

unread,
May 3, 2012, 5:00:54 PM5/3/12
to sqle...@googlegroups.com

On Wed, May 2, 2012 at 9:38 AM, Gaurav Gupta <gaura...@gmail.com> wrote:
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?

 
try calling  cleanup_all 
also It seems wierd to me that you call setup_all before you connect the DB, but if it works...

Gaurav Gupta

unread,
May 3, 2012, 10:54:03 PM5/3/12
to sqle...@googlegroups.com
On Thu, May 3, 2012 at 2:00 PM, yoav glazner <yoavg...@gmail.com> wrote:

On Wed, May 2, 2012 at 9:38 AM, Gaurav Gupta <gaura...@gmail.com> wrote:
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?

 
try calling  cleanup_all 

cleanup_all() would resolve the issue (I had tried it already) but its not the right fix. It would destroy all existing sessions to the db.

 
also It seems wierd to me that you call setup_all before you connect the DB, but if it works...

 I did not pay attention to it before, yup its wierd :). I moved it after I create the engine in my code and I have the same problem. 

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

Reply all
Reply to author
Forward
0 new messages