11

I defined two models:

class Server(models.Model):
    owners = models.ManyToManyField('Person')

class Person(models.Model):
    name = models.CharField(max_length=50)

admin.site.register(Server)
admin.site.register(Person)

After that I even checked the sql, just for fun:

BEGIN;
CREATE TABLE "servers_server_owners" (
    "id" integer NOT NULL PRIMARY KEY,
    "server_id" integer NOT NULL,
    "person_id" integer NOT NULL,
    UNIQUE ("server_id", "person_id")
)
;
CREATE TABLE "servers_server" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL,
    "port" integer unsigned NOT NULL,
    "state" integer NOT NULL
)
;
CREATE TABLE "servers_person" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL
)
;
COMMIT;

There it even says CREATE TABLE "servers_server_owners"

I ran syncdb to install the new models to the database. I went to the admin-interface to define some objects to play with, but I got the following error:

DatabaseError at /admin/servers/server/1/  
no such table: servers_server_owners

I shutdown the dev-server, ran syncdb again, started the server: Still same problem. Why can't it find, the table, even though it just told me it created id?

perror
  • 7,071
  • 16
  • 58
  • 85
varesa
  • 2,399
  • 7
  • 26
  • 45
  • 1
    Either you have missing information in the above, or your example is wrong. The default intermediate table should be "servers_server_person" unless you defined the intermediate table using "through" – James R Apr 09 '12 at 18:22
  • @James I have not used `through`, and I also thought about the same thing when I saw the sql. It seems to be picking up the name of the property – varesa Apr 09 '12 at 18:32
  • @varesa: The correct way to answer your own question on SO is to post it as an answer and accept it, not edit your original question. Welcome to Stack Overflow! – cha0site Apr 09 '12 at 19:30
  • @cha0site Yes, I know and already tried. There only happens to a 8 hour delay before people under 100 reputation can answer their own questions. I only edited to save others who view this question the trouble trying to answer to this, until I can post the answer. Six hours to go. – varesa Apr 09 '12 at 19:51
  • 1
    `python manage.py migrate` solve the issue for me – Arefe Jun 04 '16 at 05:08
  • @Arefe Yeah, that is the way to go nowdays. Migrate did not exis four years ago so you had to use external tools like South mentioned below – varesa Jun 04 '16 at 09:44

5 Answers5

9

As a tip for the future, look into South, a very useful utility for applying your model changes to the database without having to create a new database each time you've changed the model(s).

With it you can easily: python manage.py migrate app_name and South will write your model changes. The documentation is pretty straightforward.

nairware
  • 3,090
  • 9
  • 37
  • 58
droidballoon
  • 720
  • 10
  • 17
8

Actually the problem was that the table never got created. Since I am fairly new with django, I did not know that ./manage.py syncdb does not update existing models, but only creates the ones that do not exist.

Because the model 'Server' existed before I added the other model, and it was already in the db, 'syncdb' did not actually create the new tables.

varesa
  • 2,399
  • 7
  • 26
  • 45
  • So how *do* you create new tables? – CodyBugstein Jun 15 '14 at 17:30
  • 1
    @Imray There are few ways, the simplest being delete and recreate the database. A more sophisticated way is to use a "migration tool". Around the time I asked the question you had to use an external tool like South, but I think a similar tool is built into django. See `droidballoon`'s answer for the older solution and `Michael`'s answer for the new one – varesa Jun 15 '14 at 17:35
4

I meet the same problem today and fix it. I think you miss some command in tutorial 1. just do follow:

./python manage.py makemigrations polls
python manage.py sql polls
./python manage.py syncdb

then fix it and gain the table polls and you can see the table created. you should read the manage.py makemigrations command.

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
Michael
  • 166
  • 1
  • 3
  • That might be the fix now, however it did not exist when I made the question. From the official docs: "Prior to version 1.7, Django only supported adding new models to the database; it was not possible to alter or remove existing models via the syncdb command (the predecessor to migrate). Third-party tools, most notably South, provided support for these additional types of change, but it was considered important enough that support was brought into core Django." I can't find the release date of 1.7, but it is probably at least a year after this was asked. – varesa Apr 20 '14 at 18:06
2

for django 1.9, this is what i did and it solved the issue.

python manage.py makemigrations app_name

python manage.py migrate

Annie
  • 21
  • 2
0
  1. use "manage.py help" to check the command
  2. if you find migrate and makemigration, it means your python has updated

step 1:

python manage.py makemigration

result 1:

Migrations for 'mainsite':
mainsite\migrations\0001_initial.py
- Create model Post

step 2:

python manage.py migrate

result 2:

Operations to perform:
Apply all migrations: admin, auth, contenttypes, mainsite, sessions
Running migrations:
Applying mainsite.0001_initial... OK

finally, runserver. Done

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56