I was able to set up a Custom User using AbstractUser.
I'm trying to find a way to use Django's Password Reset Class to reset my users. So I implemented it the way it is normally used but I get the following error:
ProgrammingError: column app_user.date_joined does not exist
or
app_user.is_active does not exist
I shouldn't have to add any of these fields to my AbstractUser and I'm sure there is a way to override the Django auth. Does anyone know how?
EDIT: I've already migrated all my databases.
Your database is not migrated.
You have to run python manage.py migrate to migrate the DB.
If that was never run, than you have to make the migrations before applying them.
python manage.py makemigrations
python manage.py migrate
Related
I have an existing django project and need to create an instance of it in a new environment with new database. I have the database connection configured in the settings file. The schema does not exist. If I run the manage.py migrate command, does it also create the schema? It looks like it assumes the schema already exists because I am getting an error django.db.utils.OperationalError: (1049, "Unknown database 'my_db'"). Just wondering if I have to create the database first or if some django command is available to create it if it does not exists.
I can create the schema manually via sql script if it's not doable via python django command.
As already pointed out in the comments to your question, the missing database is the problem, not the schema. You have to create the database first, which might involve setting the user permissions for the new database. After that, the manage.py migrate command will work just fine and create the schema for you.
I've tried all of the solutions from the top results for this question on Google; none of them work for my situation:
doing python manage.py migrate auth before `python manage.py migrate didn't work (same error)
changing all references to 'auth.User' in my models to AUTH_USER_MODEL didn't work (same error), and neither did get_user_model() (makemigrations detected no changes anyway)
doing python manage.py makemigrations [app_with_user_relation] did not work either (migrations were made, but still got the same error)
have deleted and recreated the database multiple times
Does anyone have any new suggestions?
I hear downgrading to Django 1.7 might work, but that is not an option for me.
If anyone is curious, the app I am trying to migrate is powerdns, from the open source package django-powerdns-manager.
Full traceback: https://dpaste.de/JdT5
The failing migration: https://dpaste.de/rfUn
Custom db router: https://github.com/mike-johnson-jr/django-powerdns-manager/blob/master/src/powerdns_manager/routers.py
Custom database router routes migrations with app label "auth" to my default database rather than my powerdns database. Because of this, powerdns is making a foreignkey reference to another database, which is not possible in neither postgresql nor mysql, only sqlite. In mysql, error will be "cannot create foreign key constraint"
You can solve this error by only using 1 database or using sqlite for both.
I am working on a tutorial on Django
When I invoke the interactive shell to access the database API using the command python manage.py shell, I receive the following prompt
In [1]:from music.models import Album, Song
In [2]: Album.objects.all()
Then the next line gives me this
OperationalError: no such table: music_album
Like the error says, it means that your database has no tables that correspond to these models.
Typically that means that you forgot to migrate your database. By running:
python manage.py makemigrations
Django will construct files (in someapp/migrations/) these files describe the steps that have to be carried out such that tables are constructed with the correct columns, or later in the progress columns can be renamed, tables removed, new tables constructed. If you run the command, you will see new files popping up that describe these migrations.
But now you still did not migrate the database itself, you only constructed the files. It is for example possible that you want to add custom migrations yourself. In case the migration files are correct, you can run
python manage.py migrate
to change the database accordingly.
In order to run the migrations, the database has to be specified correctly in the settings.py file. But since you obtain an OperationalError that complains about the table not being found, I think this has already been handled (correctly).
See the Django topic on migrations [Django-doc] for more information.
Configure your database connection in your settings.py and, after creating Django models, you have to execute two commands:
python manage.py makemigrations
python manage.py migrate
Then tables will be created in the database and you will be able to use then in the shell
I'm in production and just ran syncdb but I made a mistake and want to delete what syncdb did, including all data in the tables is fine. Just a fresh start in the databases so I can run syncdb again.
(virtualenv-2.7)[root#server mysite]# python manage.py sqlclear mainapp | python manage.py dbshell
CommandError: One or more models did not validate:
app.comment: 'poster' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.
app.myuser: The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.
Recreate database is probably the best way. Also you can do it using reset_db management command from django-extensions package.
The Django 1.7 support migrate
you just run python manage.py migrate again after edit.
more features to see: release note
As J0HN stated in the comments, the best thing to do to start fresh was to actually just delete the database. I thought it might mess something up, but it won't. Simply login to mysql.
drop database whatever_your_database_name;
create databse whatever_your_database_name;
Then simply sync it again with python manage.py syncdb
Maybe its lazy, but I run:
django-admin.py reset_db --router=default --noinput
I upgrade to django 1.7
and need to deal with migration
My database already exists tables
Here is my step :
1.I delete the migration in django app test
2.I remove south(from INSTALLED APPS ) in django settings.py
3.run python manage.py makemigrations test
Migrations for 'test':
0001_initial.py:
- Create model Person
- Create model Book
- Create model Artical
run python manage.py migrate test
Operations to perform:
Apply all migrations: test
Running migrations:
Applying test.0001_initial... FAKED
is it right???
How to do migration if I already have tables?
Please guide me Thank you
it is right if the tables that exist in your database have the same structure as your current models (i.e. you haven't modified your models.py file after you applied you last migrations with South). If that's the case, you can proceed without worries.
If the structure is different, you can try different approaches:
Create the needed migrations manually. You will have to write your own migration with the needed modifications. Here you can find some example code of a migrations file for altering or adding fields and here you have the complete reference of the methods you can call
Modify your database manually with phpmyadmin, raw sql or the method you prefer.
Do a python manage.py dumpdata app1 app2 app3 > data.json, remove the db, create it again and to a python manage.py loaddata data.json. This may raise some errors because the data you saved was with the old structure, you will have to deal with each error.
Or the least preferred, if the data you had is not needed, just remove the db and recreate it.
There is nothing wrong with the way you are doing it.
The reason the first migration is faked is because your tables are already created:
Run python manage.py migrate. Django will see that the tables for the
initial migrations already exist and mark them as applied without
running them.
From the documentation on upgrading from South.