HOw to rebuild Django tables? - django

I was having problems with my dB, so I dropped the tables for one of my Django apps. I then ran makemigrations for the app and tried to migrate. It said everything was up to date. It didn't re-create the dropped tables.
How can I get Django to re-build the tables?

If you only drop your app tables and need to recreate the tables again. You also need to remove all the migrations entry from you app in the django_migrations table.
After you have done that you can run migrate again. Or if you want to reset your migrations files, remove all migrations from that app and run makemigrations and migrate

You have to delete all the files in app/migrations, except __init__.py
project
app1
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
app2
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
...
Then you can run python manage.py makemigrations and python manage.py migrate

Related

How to run a migration for a specific app in Django

What I need
I added an new app in my project and a separate database for it. I have created the Models for that app and now I need to migrate this models in this separate database
app name - authors_app
database name - authors
My code
python3 manage.py makemigrations authors_app
It creates the 0001_initial.py file which contains only models from my authors_app. So this step is OK
python3 manage.py migrate authors_app 0001_initial --database=authors
And this command runs not only my migrations from authors_app, but also migrations from my other app
Problem
I need to migrate only migrations from authors_app. But the migrate command runs the migrations from all apps. I have 58 migrations in my other app. And this command runs them all into the new database ...
Question
How can I run migrate for only authors_app
Update
Inside my authors_app Models I use one model from another app
from tibrains_app.models import Language
class AuthorLanguage(models.Model):
author = models.OneToOneField(Author, on_delete=models.CASCADE)
native_list = models.ManyToManyField(Language, related_name='author_native_languages')
all_list = models.ManyToManyField(Language, related_name='author_all_languages')
writing_list = models.ManyToManyField(Language, related_name='author_writing_languages')
And inside my 0001_initial file I have a dependencies
dependencies = [
('tibrains_app', '0058_book_history_period'),
]
Could this cause the problem?

Django makemigrations not detecting project/apps/myapp

Solved
Interesting:
./manage.py makemigrations apps.myapp -> "App 'apps.myapp' could
not be found. Is it in INSTALLED_APPS?"
./manage.py makemigrations myapp -> Works.
./manage.py makemigrations -> "No changes detected" (it does detect changes if myapp is in the project root)
Original question:
Project structure
myproject/
myproject/
apps/
myapp/
__init__.py
admin.py
models.py
urls.py
views.py
tests.py
myapp2/
myapp3/
__init__.py
static/
templates/
virtualenv/
manage.py
myproject/apps/myapp/models.py
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
Settings.py
INSTALLED_APPS = [
# ...
'apps.myapp',
'apps.myapp2',
'apps.myapp3',
]
makemigrations cannot find "myapps" that's not in the project root.
At the same time, migrate does find it.
$ ./manage.py makemigrations apps.myapp
App 'apps.myapp' could not be found. Is it in INSTALLED_APPS?
$ ./manage.py migrate apps.myapp
CommandError: App 'apps.myapp' does not have migrations.
Isn't the "let's put our apps into an apps folder" practise valid any more, am I doing something wrong, or is it a bug of the makemigrations command?
Note1: before running makemigrations, I removed the "migrations" folder of myapp, but I'm pretty sure it doesn't matter. It did get re-created when the app was in the project root.
Note2: I did research google and stackoverflow, I only found similar questions where the solution was either "adding the app to settings.py", "running makemigrations before migrate" or "some migration issue between Django 1.6 and 1.7". None of these apply to my situation, I think.
You may have deleted the migrations folder inside the app or __init__.py inside the <app>/migrations/ folder, create a new one
myproject/
apps/
myapp/
migrations/
__init__.py
You can always do makemigrations seperately without doing the above step
python manage.py makemigrations myapp
try
INSTALLED_APPS = [
# ...
'myproject.apps.myapp',
]
and
python manage.py makemigrations myapp
It's works now. It didn't work before because I didn't make any changes to my model.
I'm new to django and was trying to execute the command from the videos I watched. Django documentation tells you that makemigrations create new migrations based on the changes you made to your model. So, I make changes in models.py and admin.py and it's working now.
I had the same problem. I solved it by adding "migrations" file into the app folders.

how to switch to a new database

I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :
Create an new empty database
Updated settings.py and pointed the database name to the new one
Deleted the migrations folder under my App
Run python manage.py runserver and no errors returned
Run python manage.py makemigrations and python manage.py migrate
but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps
How should I do for this situation to move to the new database for my project?
Deleted the migrations folder under my App
This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.
So you need to run makemigrations <app_name> to at least get the initial migration.
If you were to do this again, don't delete the migrations, just change the database settings and then migrate.
Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.
python manage.py makemigrations app_name
Do this for all the apps of which you have deleted the migrations.
Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:
'new': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:
python manage.py migrate --database=new
You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:
python manage.py flush --database=new
Now export data from the old database into a json file:
python manage.py dumpdata>data.json
Import this data into the new database:
python manage.py loaddata data.json --database=new
Now you can remove the 'default' database and rename the 'new' database to 'default'.
The procedure mentioned in this answer is taken from my blog.
Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.
If your database has a new name, i.e. not "default", you need to specify it to migrate:
python manage.py migrate --database <newdb>

South is not dropping a table for app actstream

Trying to drop tables with South for app django-activity-stream and then re-add it.
Process I used to drop table:
Deleted all previous migrations for the app to start fresh
Replaced models.py with an empty file
python manage.py schemamigration actstream --initial
python manage.py migrate actstream
But when I look at the database, the tables are still there.
Why aren't the tables dropping -- am I doing something wrong?
Process I would use to drop/re-add table:
Deleted all previous migrations for the app to start fresh
python manage.py schemamigration actstream --initial
python manage.py migrate actstream --fake
Replaced models.py with an empty file
python manage.py schemamigration actstream --auto
python manage.py migrate actstream
Maybe you can omit the 3 step

How To Find Current Django App Name

I have an app. I think I know the name, instamagic, but I get django.core.exceptions.ImproperlyConfigured: App with label instamagic could not be found
when I try to do a migration of the database
How do I find my app's name?
My folder structure
instamagic
__init__.py (empty file)
manage.py
settings.py
urls.py
api/
image/
templates/
userprofile/
Have you added 'instamagic' to your INSTALLED_APPS in settings.py?
[EDIT]
When calling south migrations, use: `manage.py schemamigration [migration_name] --auto