5

Hey, I've a database already created. Now I've updated UserProfile with:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique = True, related_name = 'user')
    follows = models.ManyToManyField("self", related_name = 'follows') <-- NEW LINE

so python manage.py sqlall myapp returns me:

[...]
CREATE TABLE "myapp_userprofile_follows" (
    "id" integer NOT NULL PRIMARY KEY,
    "from_userprofile_id" integer NOT NULL,
    "to_userprofile_id" integer NOT NULL,
    UNIQUE ("from_userprofile_id", "to_userprofile_id")
)
[...]

When I run python manage.py syncdb:

Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.

But the table is not created when I try to insert data into. Why? (I'm testing locally, with sqlite3)

Fred Collins
  • 5,004
  • 14
  • 62
  • 111

2 Answers2

6

manage.py syncdb will not modify existing tables to add or remove fields. You will need to either manually modify your database, or use a tool like South to create automated database migrations (which is what I highly recommend)

bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
  • `ManyToManyField` should create a new table, not add or delete column. Even with `ManyToManyField` should I us South? – Fred Collins Jun 01 '11 at 22:55
  • 1
    He can also run `manage.py reset myapp` to just delete the tables and start with fresh data. Since he is adding a field which is not required, I think he can also run dumpdata to get a fixture, reset then load in the fixture. – Keegan Carruthers-Smith Jun 01 '11 at 22:56
  • @Fred, The way it works is that `syncdb` looks at the models in your project, then has a look in the database to see if that model's table already exists. If it does, it assumes everything about that model is already configured, this means that it doesn't even see the new `ManyToManyField`. So yes, even though adding a `ManyToManyField` will just create a new table, you will still need to use South for that (or modify the database yourself). – bradley.ayers Jun 01 '11 at 23:00
  • Sure, I can create the tables manually, but how about indexes? Django create also indexes for the various table, in addition to tables. South manage this? By the way, I'll check it out in a while. – Fred Collins Jun 02 '11 at 13:10
  • From [Django 1.4 site](https://docs.djangoproject.com/en/1.4/ref/django-admin/#syncdb), "Creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created.". It **should** create the table but indeed doesn't always do so, it's probably a bug in 1.4 (never seen this in 1.3), which version was this about? Anyway, as Keegan mentioned, `manage.py reset myapp` recreates correctly the tables though. Sadly enough, Django is really lacking in that domain... – RedGlyph Apr 10 '12 at 09:35
5

have you added your app to INSTALLED_APPS in settings.pys:

settings.py

INSTALLED_APPS = (
    ... ,
    'my_app',
)

https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#installed-apps

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177