Reputation: 2189
I can't seem to be able add more table to my models.py
through python manage.py syncdb
which was used to create my initial tables, MyTable inclusive. I have tried python manage.py makemigrations
and then did python manage.py migrate --fake <appname>
and even python manage.py migrate
.
Syncdb gives me django.db.utils.OperationalError: (1050, "Table 'myapp_mytable' already exists") and migration command don't give any error but the tables are not getting created.
Models.py:
class MyTable(models.Model):
#table details
#this table is already created.
class NewTable(models.Model):
#table details
#this is the table I want to add
Note: I use Django 1.7.
Upvotes: 2
Views: 1910
Reputation: 599490
You've got yourself into an inconsistent situation by starting with syncdb, then switching to migrations. The migration generator has no way of knowing that you created the initial table outside of its functionality, so will keep trying to create it itself.
To recover, you can do this. Delete your migrations, then comment out the code for your second model. Run makemigrations
and then migrate --fake
, to get the record up to the point where you actually are. Now, uncomment your second model, and run makemigrations
again; after that, migrate
should work properly to create the second table.
In the future, make sure you always use migrations from the start. (In fact this is not a problem in more recent versions of Django, since syncdb has been removed.)
Upvotes: 5
Reputation: 14391
In your app/migration folder, you will have files like 0001_initial.py and 0002_auto_20151027_0524.py. 0001_initial.py contains changes when you first time run 'python manage.py makemigrations'. After that, whenever you run this command, files like 0002_auto_20151027_0524.py are created. There can be two possible solution for this.
In file like 0002_auto_20151027_0524.py, you will have code like this
class Migration(migrations.Migration):
dependencies = [
('testing', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='downloads',
name='display_order',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='draft_reports',
name='accepted_date',
field=models.DateTimeField(default=datetime.datetime(2015, 10, 27, 5, 24, 39, 588551), blank=True),
),
migrations.AlterField(
model_name='logentry_modified',
name='action_time',
field=models.DateTimeField(default=datetime.datetime(2015, 10, 27, 5, 24, 39, 548365)),
),
]
As code shows, it is adding field in db. You can delete table which is creating problem from here.
Second possible solution is that you can delete all your migrations. Then run command to create migrations again. You should do it before adding new tables. It will create 0001_initial.py file. Then add you new models and run command again. And run 'python manage.py migrate'.
Hope it will solve your problem.
Upvotes: 1