After delete database ,my Django does't work - python

I have some problem with my database, so I deleted db.sqlite3 and Migrations manually.
than I recreate the database by using manage.py makemigrations <appname> manage. py migrate <appname>
Everything looks normal,but when I get into localhost:8000, It is a blank page without anything (even if I never change the templates).
To my confused, I can get into the admin page.
Are there any mistakes in this process?what happened to my django app?

Now what you can do is :
1) Take backup of your code
2) Create another project with other name or same name, it will have it's own sqlite db
3) Place your code in that project and then run migrations, it will recreate your db schema.
And it should work.

Related

Django migration not applied to the DB

I had an Django2.2.3 app, it was working fine. But I had to chane the name of a field in a table, and add another field. Then I ran ./manage.py makemigrations && ./manage.py migrate. Besides the terminal prompt:
Running migrations:
No migrations to apply.
No error is throwed. But then when I go to the MySQLWorkbench to check the database, it is exactly as I didn't make any change. I tried deleting the migrations and making again, the process ends with no errors but the database don't change. I create another empty database, change the name on settings.py and make migrations and migrate again, and it worked, but when I put the old database name on the settings, it just did not work.
Can someone explain this behavior for me? There is any kind of cache for these information migrations or something? I realy want to know why this is not winrkig as I espect.
Make sure the app with the migrations is in the INSTALLED_APPS. Django won't look at the app for changes otherwise.
Adding new few fields to an existing model (table) is one reason for this problem. A way to go about this is simply as follows:
a) un-apply the migrations for that app:
python3 manage.py migrate --fake <app-name> zero
b) migrate the required migrations (you've already deleted previous migrations and you've done 'makemigrations' for the newly added column. So, you just migrate:
python3 manage.py migrate <app-name>
If the steps above didn't solve the problems, then drop the table first;
i) python3 manage.py dbshell
ii) DROP TABLE appname_tablename
close the shell and repeat a and b again.

How do I transfer test database to product database

I'm working on an Django project, and building and testing with a database on GCP. Its full of test data and kind of a mess.
Now I want to release the app with a new and fresh another database.
How do I migrate to the new database? with all those migrations/ folder?
I don't want to delete the folder cause the development might continue.
Data do not need to be preserved. It's test data only.
Django version is 2.2;
Python 3.7
Thank you.
========= update
After changing the settings.py, python manage.py makemigrations says no changes detected.
Then I did python manage.py migrate, and now it complains about relation does not exist.
=============== update2
The problem seems to be that, I had a table name Customer, and I changed it to 'Client'. Now it's complaining about "psycopg2.errors.UndefinedTable: relation "app_customer" does not exist".
How can I fix it, maybe without deleting all files in migrations/?
================ update final
After eliminating all possibilities, I have found out that the "new" database is not new at all. I migrated on that database some months ago.
Now I created a fresh new one and migrate worked like a charm.
Again, thank you all for your suggestions.
The migration folder is your friend. No need to delete it. You can flatten migrations if you feel the migration folder is getting too large with many migration files, but you don't need to.
If you plan to use a new database in GCP, just change the settings.py file, which is typically located in the project_name/project_name/ folder. Locate the DATABASES section to reflect the new database credentials.
Once your app is pointing at the new database, run python manage.py migrate. This will build the database schema with the necessary tables to start populating the new database.
You don't need to delete any migrations/ folder, you can use the same migration files in product database, if you don't need the test data just delete the database i.e. db.sqlite3 (I guess you are using default database).
Note: Migration files don't effect your database structure(if you delete and do makemigrations or use the existing one it remains the same)

Database Api Python/django

I am working on a tutorial on Django
When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt
In [1]:from music.models import Album, Song
In [2]: Album.objects.all()
Then the next line gives me this
OperationalError: no such table: music_album
Like the error says, it means that your database has no tables that correspond to these models.
Typically that means that you forgot to migrate your database. By running:
python manage.py makemigrations
Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.
But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run
python manage.py migrate
to change the database accordingly.
In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).
See the Django topic on migrations [Django-doc] for more information.
Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:
python manage.py makemigrations
python manage.py migrate
Then tables will be created in the database and you will be able to use then in the shell

Switching django-cms project from sqlite to postgres

I'm trying to switch database backends for an existing django-cms project, from sqlite3 to postgresql. When I start with a fresh sqlite database and apply all migrations everything works fine. If I do the same with a fresh postgres database everything appears to go ok but I get the following error when trying to do anything:
django.db.utils.ProgrammingError: relation "cms_urlconfrevision" does not exist
LINE 1: ...sion"."id", "cms_urlconfrevision"."revision" FROM "cms_urlco...
I get a warning while running runserver that there are unapplied migrations despite the fact that a listing of migrations shows all applied, and running migrate again does nothing (makemigrations also does nothing). The cms_urlconfrevision table exists in the database, with the id and revision fields, so I'm at a loss for where to look further.
Adding versions: django 1.9.7, django-cms 3.4.4
After choosing a new database, first comment out all the other apps you have put in the INSTALLED_APPS. Then makemigration and migrate. If that works then add other apps and create migrations and migrate.
If still it doesn't work.
Try to delete all the earlier created migrations from migrations directory in every app. And then again makemigrations and migrate.

South cannot find my models

I'm having trouble trying to add south to an existing django project. Here is my situation:
My project is called alerts. Here is my folder structure:
djangoprojects/
alerts/
manage.py
alerts/
__init__.py
urls.py
views.py
models.py
wsgi.py
settings.py
common/
__init__.py
models/
__init__.py
models.py
The file alerts/alerts/models.py has the following content:
from common.models.models import *
In other words, the only thing that alerts/alerts/models.py does is import the "real" models from alerts/common/models/models.py, which has a large number of models that were created from a past project.
I also have a MYSQL database that I'll call 'existing_database'. This is the database that corresponded to the models on my past project, which ran on another machine. On that other machine I used mysqldump to get the sql file for this database and copied it over to my new machine. Then I used mysql directly to create existing_database on my new machine. So now the database existing_database should match the models in alerts/common/models/models.py.
So next I went through the documentation to try to add south to the alerts project. This is what I did:
I added 'south' and 'alerts' to the INSTALLED_APPS inside alerts/alerts/settings.py. I'm not sure why I have to add 'alerts' to settings.py inside the alerts project - you'd think the fact that the settings file was in the project folder would be enough. But it complained if I didn't add it explicitly, so I did.
I cd-ed to djangoprojects/alerts and ran ./manage.py syncdb. It said it synced some stuff, including south and alerts
Then I ran ./manage.py convert_to_south alerts. This is where I ran into problems. It said:
This application has no models; this command is for applications that already have models syncdb'd.
Make some models, and then use ./manage.py schemamigration alerts --initial instead.
So I tried running ./manage.py schemamigration alerts --initial. It created an initial migration but that migration files has almost nothing in it. All it has is a class Migration. None of my real models are anywhere to be found.
To see if it can recognize any new models, I went to common/models/models.py and added a dummy model. Then I ran ./manage.py schemamigration alerts --auto and the response I got back was "Nothing seems to have changed". So it apperas it still knows nothing about my real models.
Ao at this point I'm totally confused. Like I said, I have an existing database with real data, and I have an existing set of models code that should exactly represent that actual table structure. I just want to start using this in my alerts project and I need south because I will be adding and changing models.
How do I accomplish this?

Categories

Resources