Problem importing declarative_base from another module

1,073 views
Skip to first unread message

Brendan Condon

unread,
Mar 9, 2013, 1:37:12 PM3/9/13
to sqlal...@googlegroups.com
Everything was working fine, but I started to get this error when I run deal.py:

:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py:1343: SAWarning: The classname 'Deal' is already in the registry of this declarative base, mapped to <class 'plexus.cfm.deal.Deal'>
  _as_declarative(cls, classname, cls.__dict__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 487, in runfile
    execfile(filename, namespace)
  File "C:\env\plexus\plexus\cfm\deal.py", line 33, in <module>
    class Deal(Base):
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1343, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1244, in _as_declarative
    **table_kw)
  File "C:\Python27\lib\site-packages\sqlalchemy\schema.py", line 305, in __new__
    "existing Table object." % key)
sqlalchemy.exc.InvalidRequestError: Table 'deal' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

I have two modeles data.py and deal.py.  I use data.py to run my queries and define the declarative_base.  There are other models that import it, so I decided to keep it in a central place.

Here is the start of data.py

from sqlalchemy import create_engine

from zope.sqlalchemy import ZopeTransactionExtension


from sqlalchemy.orm import (

scoped_session,

sessionmaker,

)


from sqlalchemy.ext.declarative import declarative_base


from deal import Deal


and here is Deal.py


from sqlalchemy import (

ForeignKey,

Column,

String,

Integer,

Text,

Date,

Float,

)


from sqlalchemy.orm import (

backref,

relationship,

)


from plexus.cfm.data import Base


class Deal(Base):

    # Define class


I really appreciate any help.


Thanks,

Brendan Condon

unread,
Mar 9, 2013, 1:43:52 PM3/9/13
to sqlal...@googlegroups.com

Some of the code got cut off.  Here is the full version.

Here is the start of data.py

from sqlalchemy import create_engine

from zope.sqlalchemy import ZopeTransactionExtension


from sqlalchemy.orm import (

scoped_session,

sessionmaker,

)


from sqlalchemy.ext.declarative import declarative_base

session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))


Base = declarative_base()


from deal import Deal


and here is Deal.py


from sqlalchemy import (

ForeignKey,

Column,

String,

Integer,

Text,

Date,

Float,

)


from sqlalchemy.orm import (

backref,

relationship,

)


from data import Base


class Deal(Base):

    # Define class

Michael Bayer

unread,
Mar 9, 2013, 2:06:33 PM3/9/13
to sqlal...@googlegroups.com

On Mar 9, 2013, at 1:37 PM, Brendan Condon <bre...@brendancondon.com> wrote:

> Everything was working fine, but I started to get this error when I run deal.py:
>
> :\Python27\lib\site-packages\sqlalchemy\ext\declarative.py:1343: SAWarning: The classname 'Deal' is already in the registry of this declarative base, mapped to <class 'plexus.cfm.deal.Deal'>

that warning is the key to your issues. Are you defining two classes named "Deal"? Note that in 0.8, you can have two classes named Deal in different modules, which will be kept separate, but this isn't in 0.7. However, it seems like you're using the same table name for both classes as well, which can't occur within a single MetaData object. The examples you've sent don't illustrate this happening. If Deal is only in deal.py, that suggests some system is calling upon "deal.py" twice in two different contexts. With Pyramid, I'd bet that it's scanning and finding "deal.py" as a "view" file, in addition to it already being present as a real module, but that's just a guess.



Brendan Condon

unread,
Mar 9, 2013, 2:16:03 PM3/9/13
to sqlal...@googlegroups.com
Mike,

It appears like I may have had a circular reference, possibly due to Pyramid.  I moved the Base declaration to the __init__.py file of the project and it appears to be working again.

Thanks for your help,

Brendan
Reply all
Reply to author
Forward
0 new messages