0

I have 2 classes. Student and parent. The parent class links to the student class via a models.ForeignKey(Student, on_delete=models.CASCADE) When running makemigrations django asked whether I wanted to input because there was no default. So I input "defult". Turns out it wanted a string and now whenever I run migrate it gives me the ValueError which I have no clue how to fix or what to do. I've tried deleting migrations folder and removing both models but still nope.

C:\Users\Egor\Desktop\mysite>python manage.py makemigrations
    Migrations for 'main':
  main\migrations\0028_auto_20190129_2010.py
    - Alter field student_joined on student

C:\Users\Egor\Desktop\mysite>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, main, sessions
Running migrations:
  Applying main.0007_auto_20190129_1732...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-            
packages\django\core\management\__init__.py", line 381, in 
execute_from_command_line
utility.execute()
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\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 "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\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 "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\schema.py", line 309, in add_field
self._remake_table(model, create_field=field)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\sqlite3\schema.py", line 181, in _remake_table
self.effective_default(create_field)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\backends\base\schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\related.py", line 937, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 956, in get_db_prep_value
value = self.get_prep_value(value)
  File "C:\Users\Egor\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Defult'

models code:

class Student(models.Model):
    student_name = models.CharField(max_length=200)
    student_gender = models.CharField(max_length=1)
    student_parent_email = models.EmailField()
    student_joined = models.DateTimeField('date joined', default=datetime.now())


    def __str__(self):
        return self.student_name


class Parent(models.Model):
    mother = models.CharField(max_length=200)
    father = models.CharField(max_length=200)
    stu = models.ForeignKey(Student, on_delete=models.CASCADE)

I have no clue what to do now. Please help as to how to get rid of this value error.

Thank you

Egor. L
  • 169
  • 1
  • 4
  • 15
  • 1
    Do you have data in the database that are not backed up? – raratiru Jan 29 '19 at 16:18
  • @raratiru this is still during development so I delete and redo any test data in the database. So there is no backup of data as there is nothing of value. – Egor. L Jan 29 '19 at 16:20
  • You should delete the migration file 0007_auto_20190129_1732 and re-run makemigrations. – Daniel Roseman Jan 29 '19 at 16:24
  • 1
    If you delete `main\migrations\0028_auto_20190129_2010.py` and run again `./manage.py makemigrations` it will ask you the same question so you can answer more carefully, if suitable. If you delete both the database and the **automatically created** migrations and `./manage.py makemigrations` it is most probable to run smoothly. This is an awkward approach, of course, but it works if your data are not important. – raratiru Jan 29 '19 at 16:24
  • @raratiru Sorry, I'm really inexperienced, I tried deleting the 0028_auto_20190129_2010.py and ran makemigrations and then migrate it still gave me same error. To delete the database I would delete the db.sqlite file? And then delete the migrations folder? – Egor. L Jan 29 '19 at 16:32
  • @DanielRoseman deleting 0007_auto_20190129_1732 returns an error saying it is missing the 0007_auto_20190129_1732 – Egor. L Jan 29 '19 at 16:32
  • 1
    Yes, delete the sqlite file and then from all the migrations folders, delete the *py migrations files. Do not delete the `__init__.py` files or if you delete them recreate them by creating an empty file called `__init__.py`. – raratiru Jan 29 '19 at 16:36
  • @raratiruIt worked! Thank you so much! – Egor. L Jan 29 '19 at 16:39
  • 1
    I am glad I helped! Check [this answer](https://stackoverflow.com/a/41732280/2996101) for more details, but bear in mind that `./manage.py dumpdata` has [some serius limitations](https://docs.djangoproject.com/en/dev/ref/django-admin/#dumpdata) – raratiru Jan 29 '19 at 16:40

0 Answers0