I haven't been able to figure out how to do what I want (not have to set datetime upon creation of object) and get around this error.
Django 1.11, Python 3.6. I am currently in the process of upgrading from Django 1.8.
Problem occurs when I run python manage.py migrate
File "C:\Dev\Python36\lib\site-packages\django\db\models\fields
\__init__.py", line 1423, in to_python
params={'value': value},
django.core.exceptions.ValidationError: ["Value '' has an invalid date
format. Must be in the format YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]."]
My previous models.py (In Django 1.8)
timeToServe = models.DateTimeField(default='', null=True, blank=True,
verbose_name=get_lang(None, 'time_to_serve'))
My current models.py (In Django 1.11)
timeToServe = models.DateTimeField(default=None,
null=True, blank=True,
auto_now=False, auto_now_add=False,
verbose_name=get_lang(None, 'time_to_serve'))
My previous migration file (In Django 1.8)
('timeToServe', models.DateTimeField(default='', blank=True, verbose_name='Serveringstid', null=True)),
My current migration (0021_auto_xxxx.py) (In Django 1.11)
operations = [
migrations.AlterField(
model_name='booking',
name='timeToServe',
field=models.DateTimeField(
default=None, blank=True, null=True, verbose_name='time'),
),
There doesn't seem to be an automatic solution to this. What I did to get around this problem was to:
Check what changes in the created migration file actually would result in database changes. In my case there were some and I managed to edit the migration file to reflect those changes and remove all other changes in the migration file. I was then left with changes that Django wanted to do but would not change the database.
In my next step I uninstalled the migrations. Note the fake flag.
python manage.py migrate --fake myAppName zero
I deleted all my current migration files and there respective .pyc-file
Created new migration files
Python manage.py makemigrations
I installed the new migration file. Note the fake flag.
python manage.py migrate --fake-initial
To check status of your migrations you can run
python manage.py showmigrations
Related
I am following these two references (one and two) to have a custom user model in order to authenticate via email and also to add an extra field to it.
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
mobile_number = models.IntegerField(unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
...
...
class Meta:
db_table = 'auth_user'
...
...
As you can see, I have added the db_table='auth_user' into the Meta fields of the class. Also, I have included AUTH_USER_MODEL = 'accounts.User' and User model app (i.e., accounts)into the INSTALLED_APPS in settings.py. Further more, I deleted the migrations folder from the app.
Then tried migrating:
$ python manage.py makemigrations accounts
Migrations for 'accounts':
accounts/migrations/0001_initial.py:
- Create model User
$ python manage.py migrate accounts
Which gives me an error:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.
How can I migrate from the existing django user model into a custom user model?
You have to clear admin, auth, contenttypes, and sessions from the migration history and also drop the tables. First, remove the migration folders of your apps and then type the following:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
Afterwards, you can run makemigrations accounts and migrate accounts.
The solution is to undo your existing migrations that depend on AUTH_USER_MODEL as mentioned in this answer. In case you are trying to undo migrations for admin, auth, contenttypes and sessions and you get an error like:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'Profile.groups'.
....
First of all comment out/undo AUTH_USER_MODEL in settings.py if you had changed that.
Secondly comment out/undo your django app that contains new AUTH_MODEL from INSTALLED_APPS in settings.py.
Now you should be able to undo migrations for auth, admin, contenttypes and sessions as:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
Now add your auth model app to INSTALLED_APPS and set AUTH_USER_MODEL in your settings.py again.
Run: python manage.py migrate AUTH_APP, you may need to make migrations for your auth model app as well: python manage.py makemigrations AUTH_APP
Apply all migrations that you undo by: python manage.py migrate.
You are all done.
Note: You will lose all existing users present in database.
As in my particular case, the other answers did not help (the error still occured even after I tried to drop the tables with migrate ... zero and even after I deleted the migrations folder), the following helped, but I was at the very beginning and therefore it was no problem to just delete the db.sqlite3 file which is created whenever you migrate the first time. (Depending on your settings.py you might have a different database-file).
You really can only do this if you are sure that you don't lose important data from your database file (e.g. you do not yet have much information stored in the database and it is not difficult to start over again), and you will need to migrate everything again.
Delete the existing all the tables from data base.[Note : data will be lost]
Delete pycache and migrations from all the apps.
Run migrations for your relative app
python manage.py makemigrations users
Migrate the tables to database
python manage.py migrate
You need to run:
python manage.py makemigrations accounts
Before executing the initial manage.py migrate (by initial I mean at the very first time you run migrate on your project)
it is recommended to set up your custom User model at the start of your project so you'll have the "accounts" app migrated at the same time as the admin,auth,contenttypes,sessions tables are created.
but if you have created your tables already, then you should follow the instructions as #krishna-chandak described: https://stackoverflow.com/a/53599345/5950111
you can read the docs : https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
There's a django_migrations table in your database after your previous migration which is the cause of this inconsistency.
Solution: Deleting the django_migrations table from your database.
delete the migration folder from your apps
and then perform
python3 manage.py makemigrations
python3 manage.py migrate
I know it's rather an old question, but for people googling this topic like me today, here is a solution without deleting migrations, dropping the tables, and other nasty stuff)
https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/
I also had the same problem. I followed the steps:
In models.py, i setup basic User model
# accounts/models.py
class User(AbstractBaseUser):
class Meta:
db_table = 'auth_user'
Then, i ran makemigrations command to generate migration file
$ python manage.py makemigrations accounts
Migrations for 'accounts':
accounts/migrations/0001_initial.py:
- Create model User
Next step, i inserted record has 0001_initial todjango_migrations table
$ echo "INSERT INTO django_migrations (app, name, applied) VALUES ('accounts', '0001_initial', CURRENT_TIMESTAMP);" | python manage.py dbshell
Update lastest in model
# accounts/models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
mobile_number = models.IntegerField(unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
...
...
class Meta:
db_table = 'auth_user'
...
...
I need makemigrations again
After run makemigrations, i had the next migration file.
0002_....py
Migrate agains
python manage.py migrate.
What worked for me was a solution that I pieced togeather from all the diffretent solutions given here.
I check if the database exists since I don't have an issue with an existing database, only when the database is empty.
# check if the database exists
db_ok=false
if python ./manage.py check; then
db_ok=true
fi
if [ $db_ok = true ]; then
# database exists: do a normal migrate
python ./manage.py migrate
else
# database does not exists, make and migrate users then a migrate and cleanup of the users migraton
python ./manage.py makemigrations users
python ./manage.py migrate users
python ./manage.py migrate
rm -r users/migrations/
fi
I had similar problem, where I have to introduce the custom user model in the middle of the project. So following steps helped me to solve the issue without table drop or data loss.
(1) Create an initial empty migration for the app ('accounts')
python manage.py makemigrations accounts --empty
(2) Run migrate
python manage.py migrate
(3) Update the timestamp in the django_migrations table in the database , for 'accounts' initial migration to the 'admin' initial timestamp.
UPDATE django_migrations SET applied=<<admin 0001_initial date>> WHERE app='accounts' and name='0001_initial';
(4) Now create your Custom User model (derived from AbstractUser) with out any fields, and set the table name to auth_user. You are basically re-using the existing auth user table in database.
class User(AbstractUser):
class Meta:
db_table = 'auth_user'
(4) Now run migration, and copy the migration to 0001_initial and remove '0001_initial' from the dependency array. Also remove the newly created migration file.
python manage.py makemigrations accounts
cp accounts/migrations/0002_user.py accounts/migrations/0001_initial.py
edit the file 0001_initial.py
rm accounts/migrations/0002_user.py (remove migration file)
(5) Now add your custom fields, run makemigrations and migrate as usual.
Migrate zeroing didn't help me. I had to drop the whole database:
sudo -u postgres psql
drop database YOURDATABASENAME;
create database YOURDATABASENAME;
Then:
python ./manage.py makemigrations MYAPPNAME
python ./manage.py migrate MYAPPNAME
python ./manage.py migrate
And after these I got forward..
So, This is my model SwimmingScore:
class SwimmingScore(models.Model):
team = models.ForeignKey(Team, related_name='team_swimming', on_delete=models.CASCADE)
gold = models.IntegerField(default='0')
silver = models.IntegerField(default='0')
bronze = models.IntegerField(default='0')
fourth = models.IntegerField(default='0')
points = models.IntegerField(default='0')
I used the command python manage.py makemigrations and then python manage.py migrate . So when i am opening admin through website, it is showing "no such table", i have confirmed it through python manage.py dbshell >.table , there is actually no table forSwimmingScore, but when i am re-runnung python manage.py makemigrations, it is behaving as if model is actually migrated, for crosscheck , i have altered one field, and on terminal it is actually showing it:
Migrations for 'Schedule':
Schedule/migrations/0003_auto_20180707_0815.py
- Alter field team on swimmingscore
Whats the standard procedure to handle such cases? I am totally stuck in it. I am using sqlite3 as database in Django.
Deleting dbsqlite file and all the migration files and then running python manage.py makemigrations and then python manage.py migrate worked for me. Actually this type of error happens when we use some previous used model names which we have deleted , but they still have a table in sqlite , so django cannot let the new table formed, so the error "no table found" can be there. Thanks :)
I am following these two references (one and two) to have a custom user model in order to authenticate via email and also to add an extra field to it.
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
mobile_number = models.IntegerField(unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
...
...
class Meta:
db_table = 'auth_user'
...
...
As you can see, I have added the db_table='auth_user' into the Meta fields of the class. Also, I have included AUTH_USER_MODEL = 'accounts.User' and User model app (i.e., accounts)into the INSTALLED_APPS in settings.py. Further more, I deleted the migrations folder from the app.
Then tried migrating:
$ python manage.py makemigrations accounts
Migrations for 'accounts':
accounts/migrations/0001_initial.py:
- Create model User
$ python manage.py migrate accounts
Which gives me an error:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.
How can I migrate from the existing django user model into a custom user model?
You have to clear admin, auth, contenttypes, and sessions from the migration history and also drop the tables. First, remove the migration folders of your apps and then type the following:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
Afterwards, you can run makemigrations accounts and migrate accounts.
The solution is to undo your existing migrations that depend on AUTH_USER_MODEL as mentioned in this answer. In case you are trying to undo migrations for admin, auth, contenttypes and sessions and you get an error like:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'Profile.groups'.
....
First of all comment out/undo AUTH_USER_MODEL in settings.py if you had changed that.
Secondly comment out/undo your django app that contains new AUTH_MODEL from INSTALLED_APPS in settings.py.
Now you should be able to undo migrations for auth, admin, contenttypes and sessions as:
python manage.py migrate admin zero
python manage.py migrate auth zero
python manage.py migrate contenttypes zero
python manage.py migrate sessions zero
Now add your auth model app to INSTALLED_APPS and set AUTH_USER_MODEL in your settings.py again.
Run: python manage.py migrate AUTH_APP, you may need to make migrations for your auth model app as well: python manage.py makemigrations AUTH_APP
Apply all migrations that you undo by: python manage.py migrate.
You are all done.
Note: You will lose all existing users present in database.
As in my particular case, the other answers did not help (the error still occured even after I tried to drop the tables with migrate ... zero and even after I deleted the migrations folder), the following helped, but I was at the very beginning and therefore it was no problem to just delete the db.sqlite3 file which is created whenever you migrate the first time. (Depending on your settings.py you might have a different database-file).
You really can only do this if you are sure that you don't lose important data from your database file (e.g. you do not yet have much information stored in the database and it is not difficult to start over again), and you will need to migrate everything again.
Delete the existing all the tables from data base.[Note : data will be lost]
Delete pycache and migrations from all the apps.
Run migrations for your relative app
python manage.py makemigrations users
Migrate the tables to database
python manage.py migrate
You need to run:
python manage.py makemigrations accounts
Before executing the initial manage.py migrate (by initial I mean at the very first time you run migrate on your project)
it is recommended to set up your custom User model at the start of your project so you'll have the "accounts" app migrated at the same time as the admin,auth,contenttypes,sessions tables are created.
but if you have created your tables already, then you should follow the instructions as #krishna-chandak described: https://stackoverflow.com/a/53599345/5950111
you can read the docs : https://docs.djangoproject.com/en/2.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
There's a django_migrations table in your database after your previous migration which is the cause of this inconsistency.
Solution: Deleting the django_migrations table from your database.
delete the migration folder from your apps
and then perform
python3 manage.py makemigrations
python3 manage.py migrate
I know it's rather an old question, but for people googling this topic like me today, here is a solution without deleting migrations, dropping the tables, and other nasty stuff)
https://www.caktusgroup.com/blog/2019/04/26/how-switch-custom-django-user-model-mid-project/
I also had the same problem. I followed the steps:
In models.py, i setup basic User model
# accounts/models.py
class User(AbstractBaseUser):
class Meta:
db_table = 'auth_user'
Then, i ran makemigrations command to generate migration file
$ python manage.py makemigrations accounts
Migrations for 'accounts':
accounts/migrations/0001_initial.py:
- Create model User
Next step, i inserted record has 0001_initial todjango_migrations table
$ echo "INSERT INTO django_migrations (app, name, applied) VALUES ('accounts', '0001_initial', CURRENT_TIMESTAMP);" | python manage.py dbshell
Update lastest in model
# accounts/models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
unique=True,
max_length=254,
)
mobile_number = models.IntegerField(unique=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
...
...
class Meta:
db_table = 'auth_user'
...
...
I need makemigrations again
After run makemigrations, i had the next migration file.
0002_....py
Migrate agains
python manage.py migrate.
What worked for me was a solution that I pieced togeather from all the diffretent solutions given here.
I check if the database exists since I don't have an issue with an existing database, only when the database is empty.
# check if the database exists
db_ok=false
if python ./manage.py check; then
db_ok=true
fi
if [ $db_ok = true ]; then
# database exists: do a normal migrate
python ./manage.py migrate
else
# database does not exists, make and migrate users then a migrate and cleanup of the users migraton
python ./manage.py makemigrations users
python ./manage.py migrate users
python ./manage.py migrate
rm -r users/migrations/
fi
I had similar problem, where I have to introduce the custom user model in the middle of the project. So following steps helped me to solve the issue without table drop or data loss.
(1) Create an initial empty migration for the app ('accounts')
python manage.py makemigrations accounts --empty
(2) Run migrate
python manage.py migrate
(3) Update the timestamp in the django_migrations table in the database , for 'accounts' initial migration to the 'admin' initial timestamp.
UPDATE django_migrations SET applied=<<admin 0001_initial date>> WHERE app='accounts' and name='0001_initial';
(4) Now create your Custom User model (derived from AbstractUser) with out any fields, and set the table name to auth_user. You are basically re-using the existing auth user table in database.
class User(AbstractUser):
class Meta:
db_table = 'auth_user'
(4) Now run migration, and copy the migration to 0001_initial and remove '0001_initial' from the dependency array. Also remove the newly created migration file.
python manage.py makemigrations accounts
cp accounts/migrations/0002_user.py accounts/migrations/0001_initial.py
edit the file 0001_initial.py
rm accounts/migrations/0002_user.py (remove migration file)
(5) Now add your custom fields, run makemigrations and migrate as usual.
Migrate zeroing didn't help me. I had to drop the whole database:
sudo -u postgres psql
drop database YOURDATABASENAME;
create database YOURDATABASENAME;
Then:
python ./manage.py makemigrations MYAPPNAME
python ./manage.py migrate MYAPPNAME
python ./manage.py migrate
And after these I got forward..
I want to use Django 1.7 for a new project.
and I already have database with many records.
In many Django tutorials,
it demo how to use migration system from a fresh new project.
In my case, use django-admin startapp todo
and will use a existed table named notesnote.
I use inspectdb to dump notesnote class and write it into todo/models.py
class NotesNote(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
pub_date = models.DateTimeField()
authors = models.CharField(max_length=10)
and then
python manage.py makemigrations todo
to generate todo/migrations/0001_initial.py
then
python manage.py migrate --fake todo
do a fake migrate(cause the table already existed).
Then, If I want to amend the table's field, say add a "category" field
category = models.CharField(max_length=30)
Then generate the 0002 migration diff by:
python manage.py makemigrations todo
However, when I do the migrate by
python manage.py migrate todo
I got error as below:
django.db.utils.OperationalError: no such table: todo_notesnote
Seems it add the app's name in front of the existed table.
Which steps should I do to make a usable migrations for existed table?
The documentation about integrating Django with a legacy database contains some useful advices for your use case: in particular you should add a db_table = 'notesnote' option to the inner Meta class of your model.
I am currently involved in a project where I am using Django 1.7 development version.I want to propogate changes that I make in my models (adding a field, deleting a model, etc.) into the database schema using "makemigrations" and "migrate" commmands.I added a "age" field to one of the models in my application.
country = models.CharField(max_length=50, blank=True)
address = models.CharField(max_length=100, blank=True)
postal_code = models.IntegerField(max_length=50, blank=True)
city = models.CharField(max_length=50, blank=True)
phone_no = models.CharField(max_length=25, blank=True)
skype_name = models.CharField('Skype Username',max_length=50, blank=True)
age=models.IntegerField(max_length=25,blank=True)
When I use "makemigrations" command ,the output is like---"No changes detected".I guess that "makemigrations" is not able to figure out the changes made to the schema.Any suggestions how can I make it work??
If you are adding initial migrations to an app, you must include the app name when using the makemigrations command.
python manage.py makemigrations your_app_label
If it is the first time you are migrating that app you have to use:
manage.py makemigrations myappname
Once you do that you can do:
manage.py migrate
If you had your app in database, modified its model and its not updating the changes on makemigrations you probably havent migrated it yet.
Change your model back to its original form, run the first command (with the app name) and migrate...it will fake it. Once you do that put back the changes on your model, run makemigrations and migrate again and it should work.
I have sometimes the same problem.
I manage to populate the change in the database by following :
rm -rf your_app/migrations/*
python manage.py migrate
if it doesn't work, consider a manual drop table before, if you don't have data in it.
it worked for me with django 1.7c1