Django Migrate invalid literal for int() - python

Django 1.7 Python 2.7
I already made sqlite data base, and added some recordings to data base in admin page.
Then i change my models, added a ForeignKey. Then i ./manage makemigrations django asked about default values in previous recordings, i added wrong value ("08-808"), but i think i need added some id from another model (like 1,2,3 which exist).
Then (and now) i run ./manage migrate, i have a error:
ValueError: invalid literal for int() with base 10: '08-808'
Operations to perform:
Apply all migrations: admin, contenttypes, lab, auth, sessions
Running migrations:
Applying lab.0004_auto_20141017_0226...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 160, in add_field
self._remake_table(model, create_fields=[field])
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 74, in _remake_table
self.effective_default(field)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 183, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1722, in get_db_prep_save
return self.related_field.get_db_prep_save(value, connection=connection)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
prepared=False)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 907, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/denis/WebDir/laboratory1/env/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 915, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '08-506'

int() with base 10: '08-506'. That does not sound like an int to me. Check your migration (lab.0004_auto_20141017_0226) and modify that value with a valid int. Either that or change the field type to char or something.

Related

TypeError: expected string or bytes-like object - Django

I've deployed my Django application to DigitalOcean server via nginx, gunicorn. Today I wanted to add the script models.DateTimeField(auto_now_add=True) for saving the date when message from user was sent, but during migration (after pulling the entire repository from github on ubuntu server) I'm getting following error. And I guess the problem is in migration files, but don't know how can I manipulate them on server and how to do it safely, without losing any data.
Error:
Applying pragmatechapp.0004_auto_20200328_1036...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 793, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1435, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1414, in get_prep_value
value = super().get_prep_value(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1274, in get_prep_value
return self.to_python(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1375, in to_python
parsed = parse_datetime(value)
File "/home/kerimov/pragmatechproject/env/lib/python3.5/site-packages/django/utils/dateparse.py", line 106, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
Note: I should solve this problem very quickly, please, everyone, who knows the answer, help me. I'm really depressed and stressed.
As a solution I've used deleting the migrations one by one with a command find . -path "*/migrations/{my_migration_names}.py" -not -name "__init__.py" -delete. To get name of all of my migration files you should run python3 manage.py showmigrations command.

I cannot add a datetime field in django model

Models.py
class user_Roles(models.Model):
code = models.CharField(max_length=20)
description = models.CharField(max_length=30)
permitted_menus = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True,blank=True,null=True)
I cannot add a datetime, date or time fields in models. It shows that "[u"'' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]" while migrating the database. How to fix it??
Error
Running migrations:
Rendering model states... DONE
Applying myapp.0025_auto_20160321_0913...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
old_db_params, new_db_params, strict)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 567, in _alter_field
new_default = self.effective_default(new_field)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2293, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2288, in get_prep_value
return self.to_python(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 2275, in to_python
params={'value': value},
django.core.exceptions.ValidationError: [u"'' value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format."]
After removing the created field also, it shows the same error..
Delete the migration folder. And then execute
python manage.py makemigrations
python manage.py migrate
You can modify the created field like this
created = models.DateTimeField(default=date.today, blank=True,null=True)
Check the documentation for auto_now_add

Django migrate fail after adding new field or modifying an existing one but I don't want to delete my DB columns

I have to things that bother me and make me wondering if I really understood django or not.
first of all I changed a little thing in my model :
article_country = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
blank=True, null=True,
related_name='regions_press_article')
The related name became :
related_name='country_press_article')
And when I try to migrate I have this adorable error :
Unapplying djangocms_press.0002_auto_20160229_1136...Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/var/www/webapps/lib/python3.4/site-packages/raven/contrib/django/management/__init__.py", line 41, in new_execute
return original_func(self, *args, **kwargs)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 70, in migrate
self.unapply_migration(migration, fake=fake)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 122, in unapply_migration
migration.unapply(project_state, schema_editor)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/migration.py", line 157, in unapply
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 144, in database_backwards
self.database_forwards(app_label, schema_editor, from_state, to_state)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 460, in alter_field
return self._alter_many_to_many(model, old_field, new_field, strict)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 759, in _alter_many_to_many
new_field.rel.through._meta.get_field_by_name(new_field.m2m_reverse_field_name())[0],
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 470, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 478, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 886, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/introspection.py", line 149, in get_constraints
"foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() == "foreign key" else None,
IndexError: list index out of range
So I was like "ok never mind it's not important, let's go to the next task and simply add a new Charfield to the model :
global_name = models.CharField(_('Global Name'), max_length=310, null=True)
(I precise that I undo my country change and deleted the migration file linked to it)
And what happening ? Same error showing with the migrate command.
Why is it so complicate to do so little modifications in a model? How to solve it?
Oh, by the way, I already solved this kind or problems by doing ./manage.py migrate myapp zero.
But this time I don't want to delete my app DB!

Django: change a model and migrate?

I've added an IntegerField to a Django model, but I just can't migrate it.
I type:
python3 migrate my_app
and it tells me that there are no migrations to apply. Then, of course, my admin page outputs an error because it can't find the new column! Any ideas?
Thanks
Edit: console output
Rendering model states... DONE
Applying jokes.0003_joke_no...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 147, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 702, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python3.4/dist-packages/django/db/models/fields/__init__.py", line 1868, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''

migrate error with Django 1.7 while adding new model

I created a new model names Join with three fields, email, timestamp and updated timestamp. But, the databse sync started giving me error to add default values to create table. I added some random values and now it has started giving me totally random errors beyond my understanding. Please help here.
Here's my model:
class Join(models.Model):
email = models.EmailField(default="default#default.com")
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False, default = "")
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.email
And here's the error:
(learn_django) C:\Users\ajhavery\Desktop\learn_django\source>python manage.py ma
kemigrations
Migrations for 'joins':
0004_auto_20150320_2358.py:
- Alter field timestamp on join
- Alter field updated on join
(learn_django) C:\Users\ajhavery\Desktop\learn_django\source>python manage.py mi
grate
Operations to perform:
Apply all migrations: admin, contenttypes, joins, auth, sessions
Running migrations:
Applying joins.0002_auto_20150320_2346...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\core\man
agement\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\core\man
agement\__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\core\man
agement\base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\core\man
agement\base.py", line 338, in execute
output = self.handle(*args, **options)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\core\man
agement\commands\migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\migra
tions\executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\migra
tions\executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\migra
tions\migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, ne
w_state)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\migra
tions\operations\fields.py", line 37, in database_forwards
field,
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\backe
nds\sqlite3\schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\backe
nds\sqlite3\schema.py", line 75, in _remake_table
self.effective_default(field)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\backe
nds\schema.py", line 188, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\model
s\fields\__init__.py", line 627, in get_db_prep_save
prepared=False)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\model
s\fields\__init__.py", line 1290, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\model
s\fields\__init__.py", line 1269, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\model
s\fields\__init__.py", line 1171, in get_prep_value
return self.to_python(value)
File "C:\Users\ajhavery\Desktop\learn_django\lib\site-packages\django\db\model
s\fields\__init__.py", line 1252, in to_python
params={'value': value},
django.core.exceptions.ValidationError: [u"'00:00:00' value has an invalid forma
t. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
Any help please.
When Django asks "add default values to create table", you need to create values that resonate with the TYPE of that particular column.
Your error is apparent here:
django.core.exceptions.ValidationError: [u"'00:00:00' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
The "random" values you entered must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.
Remove the DateTimeFields from the model, remigrate, and start over. This time, when you're asked to put in default values, put it a random 'datetime' (i.e. 2006-10-25 14:30:59).
Source: https://docs.djangoproject.com/en/1.7/ref/forms/fields/#datetimefield
Best,
aj

Categories