sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2,148 views
Skip to first unread message

371c

unread,
Mar 30, 2011, 9:36:00 AM3/30/11
to sqlalchemy
Hi,

I have the following setup:

myapp.models.notes.py
Note model defined here using declarative base

myapp.models.users.py
User model defined here using declarative base

myapp.models.meta.py
Base and DBSession defined here to avoid circular imports...

myapp.lib.bootstrap.py
Called to initialize the database with some initial data. The
following is done:
create an engine (sqlite:///notes.db)
call Base.create_all(bind=engine)

The Base class is the same for the models and the bootstrap.py module,
but i still get a noreferencedtableerror...

it basically doesn't create the database tables when bootstrap.py is
called..

Any ideas ?

Regards,





eric cire

unread,
Mar 30, 2011, 10:21:16 AM3/30/11
to sqlalchemy

Hi, i can actually confirm that i don't get the error when all models are in the same module. Any ideas?

On Wed, Mar 30, 2011 at 3:57 PM, eric cire <371c...@gmail.com> wrote:
After investigating further, i have the impression that the problem occurs when models are in different modules (notes.py & users.py in this case) but if the models are in the same module eg. myapp.models.__init__.py, the tables are created.

I'd still like to know why this is happening because i don't intend to put al my models in the same module..

Thanks,

King Simon-NFHD78

unread,
Mar 30, 2011, 10:39:31 AM3/30/11
to sqlal...@googlegroups.com
This is just the way Python works - code inside a module is only
executed when that module is imported. If you don't import
myapp.models.notes, then the class definitions never get executed.

One solution is to import all the sub-modules in your bootstrap.py
before calling create_all. Another is importing the submodules inside
the myapp/models/__init__.py

Hope that helps,

Simon

-----Original Message-----
From: sqlal...@googlegroups.com [mailto:sqlal...@googlegroups.com]
On Behalf Of eric cire
Sent: 30 March 2011 14:57
To: sqlalchemy
Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
not find table with which to generate a foreign key

After investigating further, i have the impression that the problem
occurs when models are in different modules (notes.py & users.py in this
case) but if the models are in the same module eg.
myapp.models.__init__.py, the tables are created.

I'd still like to know why this is happening because i don't intend to
put al my models in the same module..

Thanks,


On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c...@gmail.com> wrote:


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

371c

unread,
Mar 30, 2011, 10:59:37 AM3/30/11
to sqlalchemy
Actually that helps alot and i had infact resolved to and was in the
process of doing so (i know that about python modules.. ;) but i
might be missing something)

But, why does this work in the context of an application (eg. pylons
app). Basically, calling Base.create_all() in some init_db method of
an application
works without having to import all the modules in, say,
myapp.models.__init__.py

Suggestions are welcome, though i'm considering the question answered

Thanks alot and Regards,


On Mar 30, 4:39 pm, "King Simon-NFHD78"
<simon.k...@motorolasolutions.com> wrote:
> This is just the way Python works - code inside a module is only
> executed when that module is imported. If you don't import
> myapp.models.notes, then the class definitions never get executed.
>
> One solution is to import all the sub-modules in your bootstrap.py
> before calling create_all. Another is importing the submodules inside
> the myapp/models/__init__.py
>
> Hope that helps,
>
> Simon
>
> -----Original Message-----
> From: sqlal...@googlegroups.com [mailto:sqlal...@googlegroups.com]
>
> On Behalf Of eric cire
> Sent: 30 March 2011 14:57
> To: sqlalchemy
> Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
> not find table with which to generate a foreign key
>
> After investigating further, i have the impression that the problem
> occurs when models are in different modules (notes.py & users.py in this
> case) but if the models are in the same module eg.
> myapp.models.__init__.py, the tables are created.
>
> I'd still like to know why this is happening because i don't intend to
> put al my models in the same module..
>
> Thanks,
>

eric cire

unread,
Mar 30, 2011, 9:57:10 AM3/30/11
to sqlalchemy
After investigating further, i have the impression that the problem occurs when models are in different modules (notes.py & users.py in this case) but if the models are in the same module eg. myapp.models.__init__.py, the tables are created.

I'd still like to know why this is happening because i don't intend to put al my models in the same module..

Thanks,

371c

unread,
Mar 30, 2011, 11:18:09 AM3/30/11
to sqlalchemy
Actually that helps alot and i had infact resolved to and was in the
process of doing so (i know that about python modules.. ;) but i
might be missing something)

But, why does this work in the context of an application (eg. pylons
app). Basically, calling Base.create_all() in some init_db method of
an application
works without having to import all the modules in, say,
myapp.models.__init__.py

Suggestions are welcome, though i'm considering the question answered

Thanks alot and Regards,


On Mar 30, 4:39 pm, "King Simon-NFHD78"
<simon.k...@motorolasolutions.com> wrote:
> This is just the way Python works - code inside a module is only
> executed when that module is imported. If you don't import
> myapp.models.notes, then the class definitions never get executed.
>
> One solution is to import all the sub-modules in your bootstrap.py
> before calling create_all. Another is importing the submodules inside
> the myapp/models/__init__.py
>
> Hope that helps,
>
> Simon
>
> -----Original Message-----
> From: sqlal...@googlegroups.com [mailto:sqlal...@googlegroups.com]
>
> On Behalf Of eric cire
> Sent: 30 March 2011 14:57
> To: sqlalchemy
> Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
> not find table with which to generate a foreign key
>
> After investigating further, i have the impression that the problem
> occurs when models are in different modules (notes.py & users.py in this
> case) but if the models are in the same module eg.
> myapp.models.__init__.py, the tables are created.
>
> I'd still like to know why this is happening because i don't intend to
> put al my models in the same module..
>
> Thanks,
>

King Simon-NFHD78

unread,
Mar 30, 2011, 11:18:09 AM3/30/11
to sqlal...@googlegroups.com
Something else must be importing those modules when run from pylons. If you really want to know how they are getting imported, stick something in the module which will raise an exception when it is imported (eg type "blah blah blah" at the top of the module) and look at the traceback.

Cheers,

Reply all
Reply to author
Forward
0 new messages