34
votes

I dropped some table related to an app. and again tried the syncdb command

python manage.py syncdb

It shows error like

django.db.utils.ProgrammingError: (1146, "Table 'someapp.feed' doesn't exist")

models.py

class feed(models.Model):
    user = models.ForeignKey(User,null=True,blank=True)
    feed_text = models.CharField(max_length=2000)
    date = models.CharField(max_length=30)
    upvote = models.IntegerField(default=0)
    downvote = models.IntegerField(default=0)

    def __str__(self):
        return feed.content

What I can do to get the tables for that app ?

11

11 Answers

88
votes
  1. drop tables (you already did),
  2. comment-out the model in model.py,
  3. and ..

if django version >= 1.7:

python manage.py makemigrations
python manage.py migrate --fake

else

python manage.py schemamigration someapp --auto
python manage.py migrate someapp --fake
  1. comment-in your model in models.py
  2. go to step 3. BUT this time without --fake
19
votes

For those that may still be having trouble (like me), try this out:

Comment out all the URL's in the main app's urls.py

Then go ahead and run migrations:

$ ./manage.py makemigrations
$ ./manage.py migrate

The problem was alleviated by removing the ()'s

    solved_time = models.DateTimeField('solved time', default=timezone.now())

to

    solved_time = models.DateTimeField('solved time', default=timezone.now)

I got this answer from reddit

2
votes

I just ran migrations with the name of the app attached, for all the apps I had provisioned and that worked.

e.g. python3 manage.py makemigrations my_custom_app

After running for all of them I ran a migrate command to seal the deal. python3 manage.py migrate. That was it. I'm still wondering why django behaves this way sometimes though.

1
votes

none of the above solutions worked for me, I finally solved by

sudo systemctl stop mysql.service

sudo apt-get purge mysql-server

sudo apt-get install mysql-server

sudo systemctl stop mysql.service

In my case the code that I pulled had managed = False and I wanted the tables to be maintained by Django.

But when I did makemigrations the custom tables were not being detected or I was getting the error that the app_name.Table_name does not exist

I tried the following:

  1. delete all the migration files inside the migrations folder (except init.py file) and then makemigrations then finally migrate
  2. above 2 answers
  3. this

PS: This solution is only feasible if backup is present or data is not important or you are just started creating the tables, as purging mysql will lead to loss of data

1
votes

This is linked to the migration data in the scripts inside the project not matching with the migration scripts in the database as far as I could tell. I solved this by the following steps :

  1. Delete all the migration scripts under migration folder except __ini__
  2. Make sure that the model.py contains the same structure as the table in the database and managed=True
  3. Remove all Django Created tables like auth_user,... etc
  4. Run the following code
$ ./manage.py makemigrations
$ ./manage.py migrate

This will create the migration scripts again and will apply it to your database.

1
votes

What solved my problem in situation when manage.py setmigration and then migrate to the SQL database is not working properly did is following:

  1. Run command : python manage.py migrate --fake MyApp zero
  2. After that: python manage.py migrate MyApp

And the problem that rises with connections.py and and after running correctly the migration command is solved! At least for me.

I'm using Python (3.x), MySQL DB, Django (3.x), and I was in situation when I needed after some time of successfully creating tables in my database, that some error regarding connections.py raises. But, above commands helped. I hope they will help all those who are having these type of problems.

0
votes

I have to face same issue and there are a couple of approaches, but the one I think is the most probable one.

Maybe you are loading views or queries to database but you haven´t granted enough time for Django to migrate the models to DB. That's why the "table doesn't exist".

Make sure you use this sort of initialization in you view's code:

Class RegisterForm(forms.Form):

  def __init__(self, *args, **kwargs):
    super(RegisterForm, self).__init__(*args, **kwargs)

A second approach is you clean previous migrations, delete the database and start over the migration process.

0
votes

I tried all of the above tricks and never worked for me. I commented on all imports and URLs that called a particular Table

0
votes

I had this issue where I was playing with same database structure in production vs development. While dropping and recreating tables will probably resolve the issue, its worth checking your database itself and see if the model is actually correct. For myself I created the development database incorrectly with the table names all in lowercase while in production the first letter of tables were capitalized. I used the python manage.py inspectdb command on production db, and compared it to the model and realized that in the model it was trying to insert data to table 'test' instead of 'Test' for example. Hope that helps some of you in future.

0
votes

if the python manage.py migrate still doesn't work I mean when you do this it nothing do anything you can delete the app's migrations from django_migrations table after then do

python manage.py migrate
0
votes

I had a similar issue.

I had another python (with a class) file which need access to DB.

For some reasons, when running 'makemigrations' this file was processed (I guess this is linked to some import chains).

In this class, I had a method containing a default arg method(defaultModel=Model.get_default()) in the signature which was accessing to the default object in the DB (static method included in the Model class).

A the import time, this default arg was evaluated and as the table is not populated yet, it gives this error.

So I just set None for the default args and asks for the default model object inside the method. This solved the issue.