Django auth models not being created - python

I'm using django 1.8 in my project and it seems like the auth models are not being created. I have applied the auth migrations and I get;
python manage.py migrate --database=default auth
Operations to perform:
Apply all migrations: auth
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
But only the django_content_type table is created. Am also using Postgres.
Can anyone please give me pointers on how I can solve this.
Thank you.
edit:
running ;
python manage.py createsuper
part of error dump
django.db.utils.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
Edit
settings.py
....
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
........
DATABASES = {
'default': {
'NAME': 'dbname',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'theuser',
'PASSWORD': 'thepassword',
'HOST':'thehost'
},
'auth_db': {
'NAME': 'dbname',
'ENGINE': 'django.db.backends.mysql',
'USER': 'otheruser',
'PASSWORD': 'thepassword',
'HOST':'thehost'
}
}
....
Am using 2 database connections where the mysql database is used to authenticate existing users. The idea is to use already existing users on the mysql database on the new application.

Try migrating your auth app manually followed by default migrate command.
python manage.py migrate auth
python manage.py migrate
hope this helps.

I have solved the problem finally by removing the db routers. This is because in my application as described above, i'm routing the auth requests to another db where there are already existing.
Such that running
python manage.py sqlmigrate auth 000_initial
Produces and empty sql statement.
Removing the db routers from the settings config proceeded by running
python manage.py migrate auth
produces the 'sql statements' hence migration of downstream apps proceeds without a problem.

Related

heroku deployment, relation "django_session" does not exist

I've build a simple browser game on Python+Django, which is using session/cookies to track the score. The game it self doesn't have any database. When I'm deploying to heroku, I got this type of error
ProgrammingError at /
relation "django_session" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_session" WHERE "django_sessio...
^
Request Method: GET
Request URL: https://lesgogo.herokuapp.com/
Django Version: 3.0.7
Exception Type: ProgrammingError
Exception Value:
relation "django_session" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_session" WHERE "django_sessio...
^
Exception Location: /app/.heroku/python/lib/python3.8/site-packages/django/db/backends/utils.py in _execute, line 86
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.8.3
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python38.zip',
'/app/.heroku/python/lib/python3.8',
'/app/.heroku/python/lib/python3.8/lib-dynload',
'/app/.heroku/python/lib/python3.8/site-packages']
Server time: Sat, 13 Jun 2020 04:00:23 +0000
what I've tried:
deployed successfully to heroku without views.py (the logic part with session in that file), static files like img and css were loaded/showed properly
I was here and tried that:
python manage.py migrate --fake sessions zero
# then your sessions migrate will be
python manage.py showmigrations
sessions
[ ] 0001_initial
# then migrate with --fake-initial again
python manage.py migrate --fake-initial
added, and pushed back to heroku, didn't worked.
my settings.py file is pretty much default, so all middlewares are there, I just added my app name
INSTALLED_APPS = [
'myapp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
the versions i'm using
python-3.8.3
asgiref==3.2.7
dj-database-url==0.5.0
Django==3.0.7
django-heroku==0.3.1
gunicorn==20.0.4
psycopg2==2.8.5
pytz==2020.1
sqlparse==0.3.1
whitenoise==5.1.0
PS: it's my first post. Thanks!
After following the advice in this link ProgrammingError: relation "django_session" does not exist (it is the link you mention above), I then did
heroku run python manage.py makemigrations sessions
heroku run python manage.py migrate sessions

Django migration didn't migrate authtoken and sessions

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')]

Error when running migrate command Django

I am trying to deploy my first web project on a server. I am getting this error when running migrate and makemigrations:
ProgrammingError (1146, "Table '<user>$<dbname>.<table_name>'' doesn't exist")
Everywhere it says that you should run manage.py syncdb but this is obsolete , also when running manage.py --run-syncdb the same error is given.
models
class Book(models.Model):
name = models.CharField(max_length=350)
author = models.CharField(max_length=350)
category = models.CharField(max_length=200)
def __str__(self):
return self.name
snipppets from settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
} #Everything is replaced with the correct credentials
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main_app',
'accounts',
]
The DB is empty on the server. On my PC (when I developed it locally) it worked, it created the table for me when it didn't exist in the DB.
I am using Django 1.11 and Mysql 5.6.27.
I tried other answers but did not help me.
Can you please suggests what can I do to solve this issue?
You should use migrations mechanism: https://docs.djangoproject.com/en/1.11/topics/migrations/
./manage.py makemigrations # this creates migrations files
./manage.py migrate # this applies migrations
You don't need to use syncdb.
for the first time you creating your Django project in order to creating pre-installed apps (such as admin interface) database in django you need to use :
python manage.py migrate
it creates all necessary database tables and relations.
but every time you make changes to your app like creating new columns in your models, you can use:
python manage.py makemigrations [your app name]
the result will be something like below :
(env35) someone#shell:~/bookstore$ python manage.py makemigrations books
Migrations for 'books':
0005_publisher_family.py:
- Add field family to publisher
and finally you run:
python manage.py migrate
to Synchronizes the database state with the current set of models and
migrations.

Django superuser fixture error - no such table: auth_user

I want to define a fixed username and password for superuser creation in Django's syncdb (after it has been executed). The method I'm using below was working in an older version of Django (I guess 1.6) but now it's not working.
I have this fixture file initial_data.json :
[
{
"fields": {
"username": "me",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": null,
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$...",
"email": "a#a.co",
"date_joined": "2015-04-23T01:13:43.233Z"
},
"model": "auth.user",
"pk": 1
}
]
when I add this in settings.INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_app',
)
and run python manage.py syncdb, I get the following error :
django.db.utils.OperationalError: Problem installing fixture 'path/initial_data.json': Could not load auth.User(pk=1): no such table: auth_user
What should I do?
Can I change the order that fixtures load to ensure that auth_user table is created before this fixture is loaded?
Or any other way to automatically create a superuser in Django?
Thanks in advance.
I solved my problem, however it is definitely not the prettiest solution.
I saw that when I run heroku run python manage.py syncdb, I get the following
Operations to perform:
Synchronize unmigrated apps: myproject, permissions, myapp1, myapp2
Apply all migrations: auth, flatpages, admin, contenttypes, sessions, sites
Synchronizing apps without migrations:
Creating tables...
Creating table app1_model1
Creating table app1_model2
...
So I thought what if i migrate the included apps in reverse order :
heroku run python manage.py migrate sites; heroku run python manage.py migrate sessions; heroku run python manage.py migrate contenttypes; heroku run python manage.py migrate admin; heroku run python manage.py migrate flatpages; heroku run python manage.py migrate auth;
and it worked! I have no idea why, but it did. Maybe this will help someone else too.

Using Django ORM inside Tornado, "syncdb" doesn't work

I'm using Django ORM inside Tornado, and everything is going well except that "syncdb" doesn't work.
So this is my directory structure:
APP_NAME/
APP_NAME/
models.py
settings.py
__init__.py
manage.py
database.db
When I do python manage.py syncdb, the message shows:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
And the database.db file is created (I'm using splite3), but no tables are created. It's worth noting that if I introduced some syntax error into models.py, syncdb would complain, so it seems like syncdb is able to find my models.py. It's just that it's not creating tables for some reason.
For your reference, here is my settings.py (the only thing I omitted is SECRET_KEY):
INSTALLED_APPS = (
'APP_NAME',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'database.db',
}
}
Thank you.
P.S. I'm not sure if this is relevant, but I'm using Python 3.3.1 + Django 1.5.1 + Tornado 3.0.1
if you did
django-admin startproject foobar
then your settings should look like :
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'foobar',
)
then
python manage.py syncdb
will give you
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table foobar_mymodel

Categories