Django migration didn't migrate authtoken and sessions - python

When running python manage.py migrate not all migrations were run, specifically django_celery_results, authtoken and sessions. This resulted in the application related migrations erroring out.
However, if I first manually migrate those three, and then specifically migrate auth (not sure why I'd need to migrate that again) and then do python manage.py migrate it'll work.
The installed apps on Django are like so:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'django_celery_results',
'celery.contrib.testing.tasks',
'api_app'
]
I'm wondering why that's happening, I thought migrate will run all the migrations listed in "operations to perform".

Your api_app.0002 migration creates a user without setting last_login. Therefore this migration must be run after the auth 0005 migration that allows nulls in this column.
If you add a dependency to your migration, then Django will run them in the correct order.
class Migration(migrations.Migration):
dependencies = [('auth', '0005_alter_user_last_login_null')]

Related

how to solve this problem in virtual environment?

i want to activate my models in my files, my code in terminal:
(env) PS C:\Users\MadYar\Desktop\code.py\learning_log> python manage.py makemigrations learning_logs
error:
No changes detected in app 'learning_logs'
im using django to make a web application and i add models in my settings.py like that:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'learning_logs'
]
i dont know why it sais no changes detected in app 'learning_logs'
add empty __init__.py in learning_logs folder.
check if you have any models.py in learning_logs folder.
add some models, which inherited from django.models.Model:
from django import models
class MyModel(models.Model):
myfield = models.Charfield(max_length=255)
call python manage.py makemigrations learning_logs
if you see No changes detected in app 'learning_logs' again, try to read the tutorial, especially this part: https://docs.djangoproject.com/en/4.0/intro/tutorial02/

Apply migrations and models from all the apps

I'm using Django and I have an schema like
mainapp
|---mainapp
| |---migrations.py
| |---models/
|---app2
|---migrations/
|---models/
But, when I execute:
python manage.py migrate it is generationg the tables of mainapp/models, but no the app2/models and app2/migrations either.
How can execute those migrations?
first of all try
python manage.py makemigrations
for a specific app
python manage.py makemigrations appname
this will migrate all the apps
then
python manage.py migrate
hope it helps
Make sure you have added the app in the installed apps.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainapp',
'app2',
#......,
#......,
]
Then create migrations
using python mananage.py makemigrations and migrate with python manange.py migrate

how to setup sites framework in django

I am trying to setup multiple sites from a single project in django. I was trying to set this up following https://docs.djangoproject.com/en/1.8/ref/contrib/sites/ but there is no mention of setup only how to use the sites framework once implemented.
I have implmented sites before and I added sites to my installed apps like this:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
I tried to issue the command: python manage.py makemigrations but it says:
No Changes detected
I'm using django 1.8 , does the sites framework setup still create a table called django_site ?
How do I do the initial setup to have this table created?
I guess I needed to just migrate instead of make migrate like this:
python manage.py migrate

Test Django app using models from a second app

When I run my Django tests for an app, only the models for that app are loaded.
My app has a dependency on a second app and requires that the tables for the second app's models be present in the database.
How is this achieved?
Each time your create an app, you need to add it to the installed apps, for do that open your setting file, and add your app to installed apps:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your_test_app',
'here_goes_your_other_app'
)
Then close the server, run python manage.py syncdb and try.

migrate command in south cause this error:table "model_name" already exist

I have a project named HubHub that contains 2 apps named DrHub and AgencyHub,when changing models syncdb doesn't change them and I tried to use south :
in settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'grappelli',
'django.contrib.admin',
'south',
'AgencyHub',
'DrHub',
)
I ran the first command to config first migration based on this tutorial: http://south.aeracode.org/docs/tutorial/part1.html
python manage.py schemamigration DrHub --initial
and the second command:
python manage.py migrate DrHub
but this command cause this error:
table "model_name" already exist
"model_name" is the name of first model of models.py in DrHub
If you found any solution then post answer.
thanks in advance
It`s because initial migration will create all tables in database for you. And you have an existing database with existing tables. You can either wipe you database and then do a migrate or you need to use a --fake option in migrate. Docs here
python manage.py migrate DrHub --fake
Please remove the database table and try to create sync db.

Categories