How do I reinstall the db.sqlite3 file in django - python

I am a beginner with django and I accidentally deleted the db.sqlite3 file in my django project.
I made a new django project but the db.sqlite3 file was not in it. I also uninstalled and reinstalled django and project but the db.sqlite3 file still wasn't there in the project. What do I do.
Thank you.

I am a beginner with django and I accidentally deleted the db.sqlite3 file in my django project. I made a new django project but the db.sqlite3 file was not in it. I also uninstalled and reinstalled django and project but the db.sqlite3 file still wasn't there in the project. What do I do. Thank you.
Delete everything except init.py file from migration folder in all django apps
Make changes in your models (models.py).
Run the command python manage.py makemigrations or python3 manage.py makemigrations
Then run the command python manage.py migrate.

Related

how to run django-simple-blog?

How to run Django-simple-blog? I tried to install django-simple-blog but could not find the manage.py file to run it. Can I get a solution or another simple blog?
Django has a concept of apps and a concept of projects. A project will have a manage.py file like you mention, and will also have a settings.py file that declares all of the apps that the project uses.
django-simple-blog is an app, meaning you install it within an existing project. After this explaination, the rest of the steps found here should be easier to follow: https://github.com/drager/django-simple-blog/blob/master/README.rst
The remaining steps are to:
Add 'simpleblog' to INSTALLED_APPS in your settings.py file
run the command python manage.py migrate from your project root
include 'simpleblog.urls' into any of your urls.py file

Start over with django migrations

I have 2 related questions.
I have deleted all my migrations, including the migrations directory, from all the apps in my project. I've started with a clean database. But still, when I run ./manage.py makemigrations, django says there are no changes to be made. How do I completely start over with migrations? No squashing, just starting over.
It seems that when I call makemigrations, django consults the database. I'd like my codebase to be the only source of truth for my migrations. Is there a way to do this?
If an app doesn't have a migrations/ directory with an __init__.py file (even on Python 3), Django won't create any migrations if you don't specify the app name. You either have to create the directories and __init__.py files, or you have to call ./manage.py makemigrations <app_name> for each app, which forces Django to create the directory.
It doesn't. It may connect to the database during the makemigrations command, but the migrations are purely based on your models.

Reset SQLite database in Django

I am trying to refactor a Django project. I renamed a couple apps and added a new one, as well as shuffled some models around. I want to clear my database and migrations and start fresh, but I am not sure how to accomplish this. Here's what I did:
rm -r myapp/migrations // I ran this for all my apps
python manage.py flush
python manage.py makemigrations myapp // I ran this for all my apps
python manage.py migrate // This errors
I get an error:
django.db.utils.OperationalError: table "myapp_mymodel" already exists
Can anyone tell me what I might be doing wrong?
EDIT: What is the django command to delete all tables? did not work.
Delete database and delete migration files (.py and .pyc) in migrations directory of your app (don't delete __init__.py file). Then run python manage.py makemigrations app and python manage.py migrate.
I had the same issue, using Django 1.10, here is what I did, I deleted the database sqlite file, deleted the pycache folders inside each of the apps, deleted all files inside the migrations folder for each app , except the init.py file, and then ran python manage.py makemigrations and python manage.py migrate. Also note that because you deleted the database you will have to create a new superuser using python manage.py createsuperuser. Hope this helps
For me, just
python manage.py flush
deleted old db contents, so i was able to create records anew in Django 2.1.4.
Don't forget to create new superuser:
python manage.py createsuperuser
This may help you if you want to clear sqlite3 DB follow these steps.
Delete migrations files except init.py
Delete dbsqlit3 file
Then type python/python3 manage.py migrate
Then make changes in your models
Type python/python3 manage.py makemigrations
Type python/python3 manage.py migrate
Then you have to create new superuser by just typing python/python3 manage.py createsuperuser . you should use new name not old user name
Do not delete your database file!
Its correct to delete migration files and then run flush but deleting sqlite database file is wrong. This worked to me every time. If you are using other database, it will save you a lot of work and preparations.
delete all ".py" and ".pyc" files manually
python manage.py flush
type "yes" to confirm
python manage.py makemigrations
python manage.py migrate

Reinstalling Django App - Data tables not re-created

I am trying to reinstall one of my apps on my project site. These are the steps that I have followed to do so:
Removing the name of the installed app from settings.py
Manually deleting the app folder from the project folder
Manually removing the data tables from PostgreSQL
Copying the app folder back into the project folder; making sure that all files, except __init__.py is removed.
Run python manage.py sqlmigrate app_name 0001
Run python manage.py makemigrations app_name
Run python manage.py migrate app_name
Run python manage.py makemigrations
Run python manage.py migrate
However, after all these steps the message I am getting is that there are "no changes detected" and the data tables have not been recreated in the database, PostgreSQL.
Am I missing some additional steps?
I think I might have managed to solve the problem. The command, python manage.py sqlmigrate app_name 0001, produces the SQL statements required for the table creation. Thus, I copied and paste the output into the PostgreSQL console and got the tables created. It seems to work for now, but I am not sure if there will be repercussions later.

South Migration Error

I am trying to learn Django by setting up one of the projects I found on github. Afetr I ran the syncdb command it showed
Not synced (use migrations):
- django_extensions
- djangoratings
- profiles
- guardian
(use ./manage.py migrate to migrate these).
When I am running "python manage.py migrate app" , it gives
AttributeError: 'module' object has no attribute 'Migration'.
I also ran schemamigration app --auto and --initial as well. But nothing seems to be working. Can somebody point out where I am going wrong.
Are you actually running python manage.py migrate app exactly? If you want to migrate all apps, just run python manage.py migrate. (without app) If there is an app actually named app that you want to run migrations for, then you would do what you did.
If you still get the error after running python manage.py migrate, then there must be an invalid migration file somewhere. I would migrate each app individually like this:
python manage.py migrate django_extensions
python manage.py migrate djangoratings
... etc.
to find the app with bad a migration file. Once you find the app, look in the migrations folder of the app to find any empty migration files.
First thing you must run initial migration.
python manage.py schemamigration --initial
You are getting this error because of your project structure or there is some issue with your south package .try to remove and reinstalling south.
You need to see that there should not be any extra .py file in your migration folder.

Categories