Opened 16 years ago

Closed 15 years ago

#8747 closed (fixed)

Importing models from within `__init__.py` prevents syncdb from creating tables

Reported by: Julien Phalip Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a very simple app with this structure:

myapp/
    __init__.py
    models.py

That app wouldn't create its tables when running syncdb. After poking around I realised that it was because within the __init__.py I was importing the models:

# __init__.py

from models import *
...

So, my question: is that a case of "don't do that", and if so is that worth specifying in the documentation?

It took me quite a while to debug that, and I found nothing obvious in the docs to help me (if there is, please point me to it).

Change History (6)

comment:1 by Julien Phalip, 16 years ago

I could add that this is correct Pythonically and generates no exception. It's just that syncdb won't create the tables. If it's by design, then some clue should be given in the code or doc to help debug that.

comment:2 by Jacob, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted

comment:3 by Luc Saffre, 15 years ago

I had probably the same problem, but less clear. I also imported models from the application's __init__.py, but the problem appeared only when I added another class definition (not a Model). I used manage.py testserver. It seemed that the whole application didn't get "activated" (also fixtures were not loaded) when this additional class definition was there. But no error message, not even when using -v 2. Also no error when importing these modules from a shell.
It stopped when I moved the additional code to another module and not importing it in __init__.py

comment:4 by Arthur Koziel, 15 years ago

I can reproduce this with 1.0 but not with a recent trunk checkout (rev 10688).

comment:5 by Arthur Koziel, 15 years ago

Okay, this bug was fixed in r10088

comment:6 by Alex Gaynor, 15 years ago

Resolution: fixed
Status: newclosed

Apparently the patch from the past fixed this. r10088

Note: See TracTickets for help on using tickets.
Back to Top