25π
The issue is likely related to this open bug in Django. You have some test data in one of the fields that you are now converting to a ForeignKey.
For instance, maybe department
used to be a CharField
and you added an employee who has βtestβ as their department
value. Now youβre trying to change department
from a CharField to a ForeignKey. The issue is that Django is trying to convert the previous value βtestβ into a relational value (integer) for the ForeignKey.
I can think of a few good solutions:
- If this is just a test database, just reset your database and run the migration on a clean database
- If you need to migrate the existing data, figure out what field has the βtestβ value. Then try something similar to the solution given in the bug report:
β`
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('documents', '0042_auto_19700101-0000'),
]
operations = [
migrations.RunSQL('ALTER TABLE documents_document_tags ALTER tag_id TYPE varchar(32);'),
]
2π
In my case, I have the same issue on the development. This command works for me.
python manage.py flush
Make sure it removes all data from the database. Run this command, it will delete all data from the database and run migration again.
python manage.py migrate
1π
In my case error was thith replace IntegerField
to `TextField in file migartion
file migration:
# Generated by Django 3.1.3 on 2020-12-03 11:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('adminPanel', '0028_auto_20201203_1117'),
]
operations = [
migrations.RemoveField(
model_name='myuser',
name='balance',
),
migrations.AlterField(
model_name='myuser',
name='bot_admin_chat_id',
field=models.IntegerField(default=0),
),
]
I change IntegerField
to TextField
& python manage.py migrate
After that, I remove my column in models, makemigration & migrate
After that, I do thth my model that ever I want.
- Django query how to write: WHERE field LIKE '10__8__0__'?
- GAE and Django: What are the benefits?
- Django Deployment: Handling data in database
- Select ForeignKey field in form ChoiceField
- Django β model unicode() show foreignkey object attribute
0π
The simplest way that works for me is changing the foreign key to a character field, Make migrations, migrate. Then change back the field to be a foreign key. This way, you will force a database alteration which is very important
- Django Apache Redirect Problem
- Remove line breaks from Django template
- Overriding Django's RelatedManager methods
0π
In my case @YPCrumble was correct and it was caused after I changed a field from a StreamField to a CharField.
The quickest solution I found was to open up my DB on a GUI (Postico in my case) and overwrite the values from JSON to the default value. This meant setting every existing fields values to 1, in my situation.
This let the migration run as intended, without issue.
- Django StaticFiles and Amazon S3: How to detect modified files?
- Django: Foreign Key relation with User Table does not validate
- Django cache framework. What is the difference between TIMEOUT and CACHE_MIDDLEWARE_SECONDS?
- Python Django Asynchronous Request handling
0π
It has to do with your models file.
You made use of Charfield where there was Foreignkey expectation.
You can simply flush the database to empty it, then migrate again, it will work.
python manage.py flush
Then
python manage.py migrate