On Mon, Mar 2, 2015 at 11:29 AM, Martino Io <martino8...@gmail.com> wrote: > Hello, I've been busy writing an application which now has grown > considerably, it spans across several packages/modules totalling 200K lines > of python code. > Recently I made some changes to the structure and decided to split several > classes into separate modules for greater flexibility; the problem I'm > facing now > is quite strange, before I was using mainly 1 file for all the ORM > definitions, but since I split that into several files, I've started to get > exceptions due to multiple > Base objects being defined and no shared mapper to store the metadata. I've > changed approach and started to import the base from a single module, > however given the high number of modules and cross imports I get exceptions > like: > > sqlalchemy.exc.InvalidRequestError: Table 'fabrics' is already defined for > this MetaData instance. Specify 'extend_existing=True' to redefine options > and columns on an existing Table object. > > I thought that by adding "use_existing" would fix the issue (the mapper > will ignore further imports which redefine the objects) but instead got: > > InvalidRequestError: Multiple classes found for path "Group" in the registry > of this declarative base. Please use a fully module-qualified path. > > So going back to the question, if I have a cross dependant package (which > imports classes which are ORM classes), how do I avoid the mapper > "redefining" a class? > Also any advice on best way to structure packages/modules in an ORM use case > would be appreciated. >
extend_existing is almost certainly not what you want here. Each mapped class should only be defined in a single place. If you've done that, and are still getting this error, it suggests that perhaps your Python modules are being imported more than once. Try putting "print 'importing module %s' % __name__" before your class definition. If you see the output more than once, then the module is being imported multiple times under different names. Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.