Database Management - andreasdvorak/running_results GitHub Wiki

Database creation

Create migration files

python manage.py makemigrations

Apply migrations to database

python manage.py migrate

Database clean up

Clean database via cmd

Show what would happen python manage.py sqlflush

BEGIN;
DELETE FROM "resultsapp_agegroup";
DELETE FROM "resultsapp_event";
DELETE FROM "django_admin_log";
DELETE FROM "resultsapp_distance";
DELETE FROM "auth_group";
DELETE FROM "auth_user";
DELETE FROM "django_session";
DELETE FROM "resultsapp_member";
DELETE FROM "auth_user_user_permissions";
DELETE FROM "resultsapp_result";
DELETE FROM "django_content_type";
DELETE FROM "auth_group_permissions";
DELETE FROM "auth_permission";
DELETE FROM "auth_user_groups";
UPDATE "sqlite_sequence" SET "seq" = 0 WHERE "name" IN ('resultsapp_agegroup', 'resultsapp_event', 'django_admin_log', 'resultsapp_distance', 'auth_group', 'auth_user', 'django_session', 'resultsapp_member', 'auth_user_user_permissions', 'resultsapp_result', 'django_content_type', 'auth_group_permissions', 'auth_permission', 'auth_user_groups');
COMMIT;

really clear up python manage.py sqlflush | python manage.py dbshell

delete files

delete the following files in very app

  • migrations/pycache
  • migrations/0*

Delete the database file

  • db.sqlite3

dbshell

prepare Windows

Download sqlite-tools-win32-x86 from https://sqlite.org/download.html

extract files to path

make sure the path is in the PATH variable

German: Systemeigenschaften -> Umgebungsvariablen

run dbshell

python manage.py dbshell

now you can use normal sql commands

unsorted

Abfragen der Datenbank der Klasse Post in models von der app blog

from blog.models import Post Post.objects.all()

from django.contrib.auth.models import Events Events.objects.all() me = Events.objects.get(username='admin') Post.objects.create(author=me, title='Sample title', text='Test') Post.objects.filter(author=me) Post.objects.filter(title__contains='title')

Neuerstellung von Migration und Datenbank durch Löschen von

migrations/pycache migrations/0* Dateien db.sqlite3

Dann das Datenmodel siehe oben neu aufbauen