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)

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

ManyToManyField should create a new table, not add or delete column. Even with ManyToManyField should I us South?
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.
@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).
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.
From Django 1.4 site, "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...
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

1 Comment

Well I suppose I should have just figured that out. Very sorry for trying to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.