0

I am Building a Webapp and I am stuck on an Error.

What i am trying to do

I am making a GeoDjango app using Gdal , OSGeo , Postgresql , Postgis. All of them are successfuly installed.

Tutorial :- I am following this Tutorial

When i try to open the Shop panel in Django Admin then it is keep showing me

relation "mains_shop" does not exist LINE 1: SELECT COUNT(*) AS "__count" FROM "mains_shop"

And when i delete it and migrate again

then it shows

ValueError: String input unrecognized as WKT EWKT, and HEXEWKB.

But deleting migrations is solving ValueError .

models.py

class Shop(models.Model):
    name = models.CharField(max_length=100)
    location = models.PointField()
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)

admin.py

@admin.register(Shop)
class ShopAdmin(OSMGeoAdmin):
    list_display = ('name', 'location')

settings.py

INSTALLED_APPS = [

   'django.contrib.gis',
]

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': '-------',
        'USER': '-------',
        'PASSWORD': '-------',
        'HOST': 'localhost',
        'PORT': '',
    }
}

What have i tried

  • First time when error appears then i think that GDal would not installed then I reinstalled it and it successfully installed.

  • I have reinstalled PostGis .

  • I have also seen many answers but nothing worked for me.

  • I have applied migrations many times.

  • I also tried python manage.py migrate --fake.

I don't know what am i doing wrong.

Any help would be appreciated.

Thank You in Advance.

Lars
  • 1,234
  • 1
  • 8
  • 31
  • 1
    You say you used `python manage.py migrate --fake` that might be the cause of your troubles. What that command would do is mark all migrations as applied in your database without making any changes. You should generally use `python manage.py makemigrations` and then `python manage.py migrate`. The command you used is only meant to be used if you have made the changes manually to the database. – Abdul Aziz Barkat Mar 17 '21 at 07:15
  • When i do `makemigrations` and `migrate` then it is still showing that error. When i delete all the migrations and `migrate again` then error is still showing. – Lars Mar 17 '21 at 07:17
  • The problem is all migrations are already marked as applied. I don't know actually how many migrations are really applied so I am unable to advise properly, but if no migrations are actually applied you may try running `python manage.py migrate --fake zero` to mark them as unapplied and then try migrating again. Else the only advice I can give is to drop the database and migrate again. – Abdul Aziz Barkat Mar 17 '21 at 07:20
  • When i unapply `migrations` and `migrate again` then a new error is occured **django.db.utils.ProgrammingError: relation "project_comment" already exists** – Lars Mar 17 '21 at 07:22
  • You need to find out which migrations you have actually applied and then run `python manage.py migrate --fake ` after which you need to migrate. If you can't figure that out then as I said above drop the database and migrate again. – Abdul Aziz Barkat Mar 17 '21 at 07:24
  • You mean , `reinstall postgresql` ? How can i drop the database ? – Lars Mar 17 '21 at 07:26
  • No not reinstall PostgreSQL just drop all tables in your database. check this question [https://stackoverflow.com/questions/3327312/how-can-i-drop-all-the-tables-in-a-postgresql-database](https://stackoverflow.com/questions/3327312/how-can-i-drop-all-the-tables-in-a-postgresql-database) – Abdul Aziz Barkat Mar 17 '21 at 07:29
  • Thanks you Very Much. Everything is Worked after dropping off the `DataBase` – Lars Mar 17 '21 at 07:42

2 Answers2

0

I know this is not the best solution but it works for me. This problem occurred to me after I removed my tables in database manually.

my solution:

  1. first of all I removed all of the migration files related to the model which showed in error log. ( you should remove migrations related to Shop model)
  2. change the name of models. (you can change it to Shop2)
  3. run makemigrations and migrate command
  4. change model names back. (change it to Shop again)
  5. make migrations and migrate again

after these steps the problem fixed for me. also after these step you can delete all new migration files and make migrations again to have only one migration file.

0

I was facing the same issue for one of the migration file I was generating for model. After spending couple of hours, I came to resolve it by deleting those migration files from django_migrations table.

  1. python manage.py dbshell
  2. select * from django_migrations;

You will see list of all migrations your app wise, delete all those throwing error.

  1. delete from django_migrations where id>=xx
  2. \q
  3. python manage.py migrate
Hardik Patil
  • 515
  • 1
  • 4
  • 16