Syncdb not working - django cache issue - django

I've dropped all tables from my postgres db. Now, while running
python manage.py syncdb
I'm getting error that abc fields doesn't exist in xyz table.
It's probably some sort of django cache issue. Error is of this format:
django.db.utils.ProgrammingError: relation "mmb_data_genre" does not exist
LINE 1: ...b_data_genre"."id", "mmb_data_genre"."genre" FROM "mmb_data_...
Any suggestions how to fix this?
Note - I'm using django 1.8.2 and
python manage.py makemigratons
or
python manage.py runserver
is throwing same error.

syncdb in django 1.8 is merely an alias for the migrate command but with the additional step of creating a superuser.
Deprecated since version 1.7: This command has been deprecated in
favor of the migrate command, which performs both the old behavior as
well as executing migrations.
But syncdb (migrate) should be executed only after you have done makemigrations [app_label] but in your case you seem to have the order in reverse.
Try
./manage.py makemigrations
./manage.py migrate

Related

django runserver : relation "django_migrations" already exists

I have a django project source code, which includes several apps. The source code have been run successfully on one environment, but when transplanted to another device, with the same postgresql version(9.4.4), python version(2.7.5), and django version(1.8.5), but the runserver reports errors like this. The database has been imported in advance.
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "django_migrations" already exists
Try this python manage.py migrate --fake
You can read more about it in official documentation
Try troubleshooting Initial migrations using --fake-initial
python manage.py migrate --fake-initial
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial
If you have an empty database you can clear all your migrations and then again run migrations and migrate command.
python manage.py makemigrations
python manage.py migrate

Migration in Postgresql and Django 1.8

In my project I am using django 1.8 and for a fresh project if I run
python manage.py runserver
it shows the following message:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Then if i run the command
python manage.py migrate
it works fine for sqlite.
But if i connect with postgresql in my local_settings.py and run the above migration command then it gives the following error:
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
You probably have an app that has a (generic) foreign key to ContentType. This causes migrations to fail because the database tries to create a foreign key to a table that doesn't exist yet. Try migrating contenttypes first with python manage.py migrate contenttypes and then applying your other migrations.

Recreating a database for an existing django schema

I've dropped my Postgres database by accident. Then as per this solution I deleted the migration files but now can't execute the third step's command "python manage.py migrate --fake".
RuntimeError: Error creating new content types.
Please make sure contenttypes is migrated before trying to migrate apps individually.
psycopg2.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...
^
I tried running "python manage.py migrate contenttypes" and "python manage.py makemigrations contenttypes --empty" but neither work.
Using django 1.9.5
Synchronise your project first:
like this:
python manage.py syncdb
when finish you can run: python manage.py makemigrations and last python manage.py migrate

django error OperationalError at /admin/blog/post/

i get this error in simple django programme run..
OperationalError at /admin/blog/post/
no such table: blog_post
if django version >=django 1.7
python manage.py makemigrations
python manage.py migrate
else
python manage.py schemamigrations
python manage.py migrate
so you need to migrate (map the model changes / new models into database tables) the changes so that your page works properly
delete the migrations folder in app and then do :
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
it worked for me
You need to run migrations before you can do that operation. The necessary tables don't exist in your database (as described by the error).
Check for a typo in the name of your Model. If you change your model name after makemigration and migrate, then it doesn't work and find the correct model name anymore.

DatabaseError: no such column error

So I have a model that I wanted to add ImageField to, so I typed in
picture = models.ImageField(upload_to='media/images')
I then ran syncdb and went into the shell:
python2 manage.py syncdb
python2 manage.py shell
I then imported the model and tried
"model".objects.get(pk=1)
I get the error:
DatabaseError: no such column: people_people.picture
When I run manage.py sql for the model
"picture" varchar(100) NOT NULL
is in the database.
What solutions do you guys have? I can't delete the data in the database.
As noted in the documentation syncdb doesn't add columns to existing tables, it only creates new tables.
I suggest running
python manage.py sqlall <your_app>
Looking at the sql it outputs for the table you're changing, and then running
python manage.py dbshell
in order to manually issue an ALTER TABLE command.
In future, you might like to use a migration tool like South.
There are two possibilities that to get this error 1) You added extra field to model after doing the syncdb. 2) you added new class to model.py file in django.
Solution for this is:
First install south by using command
for windows: **easy_install south** //for that you need to go to the script folder of python folder in c drive.
for linux: **sudo easy_install south**
Then follow the steps which are included here migration tutorials
step1- python manage.py schemamigration your_app_name --initial
step-2 python manage.py migrate your_app_name
Hope this will help you.
As of 1.7 migrations within Django replaces South.
Create a new set of migration instructions by running the following command in terminal:
$ python manage.py makemigrations
Check the output in the migration folder it creates to make sure they make sense then run the following terminal command to complete the migrations:
$ python manage.py migrate
That's it.
Running migrations this way allows others to implement the same migrations instead of having to manually implement db changes on every machine using the code. On the new machine all they have to run is:
$ python manage.py migrate