I changed my DB backends from sqlite3 to postgre. When I try to run migrate I have an error
MacBook-Pro-Oleg:avtofarm okorablev$ python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: messages, avtofarm, smart_selects, staticfiles, thumbnail
Apply all migrations: contenttypes, admin, callboard, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying callboard.0004_auto_20150701_1609...Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "manufdate" cannot be cast automatically to type date
HINT: Specify a USING expression to perform the conversion.
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "manage.py", line 10, in
execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/init.py",
line 338, in execute_from_command_line
utility.execute() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/init.py",
line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py",
line 393, in run_from_argv
self.execute(*args, **cmd_options) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py",
line 444, in execute
output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py",
line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 148, in apply_migration
state = migration.apply(state, schema_editor) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py",
line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py",
line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 484, in alter_field
old_db_params, new_db_params, strict) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 636, in _alter_field
params, File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 111, in execute
cursor.execute(sql, params) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py",
line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
return self.cursor.execute(sql, params) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py",
line 97, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/utils/six.py",
line 658, in reraise
raise value.with_traceback(tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: column "manufdate" cannot be cast
automatically to type date HINT: Specify a USING expression to
perform the conversion.
Model
class Zip(models.Model):
category = models.ForeignKey(Category,null=True,verbose_name='Категория')
subcategory = ChainedForeignKey(SubCategory,chained_field="category",
chained_model_field="category",
show_all=False,
auto_choose=True,verbose_name='Подкатегория')
type = models.ForeignKey(Type,verbose_name='Состояние')
zipgroup = models.ForeignKey(ZipGroup,null=True,verbose_name='Группа запчастей')
ziptype = models.ForeignKey(ZipType,null=True,verbose_name='Вид запчасти')
cartype = models.ForeignKey(CarType,null=True,verbose_name='Тип авто')
carmodel = models.ForeignKey(CarModel,null=True,verbose_name='Модель транспорта')
carbodytype = models.ForeignKey(CarBodyType,null=True,verbose_name='Тип кузова')
catalognumber = models.CharField(max_length=100,null=True,blank=True,verbose_name='Номер по каталогу')
manufdate = models.CharField(max_length=4,null=True,verbose_name='Год выпуска')
title = models.CharField(max_length=100,verbose_name='Заголовок объявления')
description = models.TextField(null=True,verbose_name='Описание')
price = models.IntegerField(verbose_name='Цена')
user = models.ForeignKey(User,verbose_name='Пользователь')
creation_date = models.DateTimeField('date published')
is_sell = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
likes = models.IntegerField(default=0)
Take a look into your migrations code (for application which contains Zip model). Looks like there is some issue with migrations, because you receive the error about date conversion, when in current model you have CharField type for manufdate column.
The problem was solved after I deleted database, made new database and one more time did makemigrations and migrate. But I think should be another way how you can decide this problem better without deleting database
Related
models.py
This is the models.py file of my django project. I am getting this error in 5 line of the code below.I wanted to connect django default User model to my model by OneToOneField to provide multiple account functionality to my project. but I am stuck at this error that I don't understand.
plz, help.
from django.contrib.auth.models import User
# Create your models here.
class DailyTrack(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, related_name = 'Profile', default='')
income = models.IntegerField()
expense = models.IntegerField()
added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'Income {self.amount} Income {self.amount}'
error is in the 4th line at the default field, I tried removing default or adding it but I am unable to understand this.
There is an error for
Operations to perform:
Apply all migrations: admin, auth, contenttypes, dailytrackapp, sessions
Running migrations:
Applying dailytrackapp.0003_dailytrack_user...Traceback (most recent call last):
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'null'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\NIitin\Desktop\MultiProj\DailyTrack\manage.py", line 22, in <module>
main()
File "C:\Users\NIitin\Desktop\MultiProj\DailyTrack\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\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\NIitin\AppData\Roaming\Python\Python39\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\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\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\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
schema_editor.add_field(
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\models\fields\related.py", line 971, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\models\fields\__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\models\fields\__init__.py", line 2388, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\NIitin\AppData\Roaming\Python\Python39\site-packages\django\db\models\fields\__init__.py", line 1776, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'null'.
NIitin#Nitin MINGW64 ~/Desktop/MultiProj/DailyTrack
$ python manage.py makemigrations
Migrations for 'dailytrackapp':
dailytrackapp\migrations\0001_initial.py
- Create model DailyTrack
NIitin#Nitin MINGW64 ~/Desktop/MultiProj/DailyTrack
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, content types, dailytrackapp, sessions
Running migrations:
No migrations to apply.```
Can somebody help me? plz, I need to fix this urgently.
I am building an app in Django.
I created a new (still empty) model having an ArrayField of DateTimeField, defined as follow:
Record_time_values = ArrayField(models.DateTimeField(), blank=False, null=False)
As I run
python manage.py makemigrations
python manage.py migrate
I get
django.db.utils.ProgrammingError: cannot cast type timestamp with time
zone to timestamp with time zone[] LINE 1: ...estamp with time zone[]
USING "Record_time_values"::timestam...
I did a little research and this is the thread that gets closest to my issue, but I don't want to go deep using SQL to fix the problem.
Does not it exist a clean way to fix the problem via pure python / Django?
In alternative, does exist a good way to store an array of DateTimeField inside a model, bypassing the arrayfield?
Complete Traceback:
(met5) C:\Users\Tommaso\Django rest framework\Udemy Django\aqi_luftdaten>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pm_lookup, sessions
Running migrations:
Applying pm_lookup.0003_auto_20200602_1142...Traceback (most recent call last):
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.CannotCoerce: cannot cast type timestamp with time zone to timestamp with time zone[]
LINE 1: ...estamp with time zone[] USING "Record_time_values"::timestam...
^
The above exception was the direct cause of the following exception:
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 "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
fake_initial=fake_initial,
File "C:\Applicazioni_Tommaso\Phyton\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:\Applicazioni_Tommaso\Phyton\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:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Applicazioni_Tommaso\Phyton\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:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\base\schema.py", line 535, in alter_field
old_db_params, new_db_params, strict)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\postgresql\schema.py", line 124, in _alter_field
new_db_params, strict,
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\base\schema.py", line 685, in _alter_field
params,
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\base\schema.py", line 137, in execute
cursor.execute(sql, params)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Applicazioni_Tommaso\Phyton\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type timestamp with time zone to timestamp with time zone[]
LINE 1: ...estamp with time zone[] USING "Record_time_values"::timestam...
Ok, reproduced. This happens when you alter a column to an array from a normal datetimefield. If you just create the field as an array field with datetimefield, then the problem does not occur.
Since the table is still empty, you should do some migration maintenance and instead of creating the field and then altering it, create it the right way. This may involve rolling back one or more migrations, that may destroy data in other tables. If that's a problem, then you should backup those tables before doing this. See Reversing migrations for details.
Hi i have a django model as below:
from django.db import models
class ClientProfile(models.Model):
id = models.AutoField(primary_key=True)
firstname = models.CharField(max_length=100,blank=False)
lastname = models.CharField(max_length=100,blank=False)
gender = models.CharField(max_length=6,blank=False)
birthdate = models.DateField(blank=False)
nationality = models.CharField(max_length=100,blank=False)
education = models.CharField(max_length=100,blank=True)
occupation = models.CharField(max_length=100,blank=True)
i have created this model with email field initially. but then i decided to deduct it.
now i get this error when i migrate:
django.db.utils.OperationalError: near "AUTOINCREMENT": syntax error
and then i deleted my database and migrated again and i get the same message again. I tried deleting my app view and then start from scrach(creating a new migration and creating a new superuser and then add the model again) and still got the same error. i even tried rollback to initial migrations and then migrate again but got the email field back again and the new migration didn't change it. what can i do?
and these are my migrations:
and this is my stacktrace:
D:\University\UT\Thesis\My Project\App\AdjustServer>python manage.py migrate
Operations to perform:
Apply all migrations: AdjustRest, admin, auth, contenttypes, sessions
Running migrations:
Applying AdjustRest.0001_initial... OK
Applying AdjustRest.0002_clientprofile_nationality... OK
Applying AdjustRest.0003_auto_20190313_2158... OK
Applying AdjustRest.0004_auto_20190313_2205...Traceback (most recent call last):
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: near "AUTOINCREMENT": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\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\Parsa\AppData\Local\Programs\Python\Python37-32\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\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\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\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 136, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 333, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 266, in _remake_table
self.create_model(temp_model)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 312, in create_model
self.execute(sql, params or None)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 133, in execute
cursor.execute(sql, params)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "C:\Users\Parsa\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "AUTOINCREMENT": syntax error
AdjustRest.0004_auto_20190313_2205:
# Generated by Django 2.1.7 on 2019-03-13 18:35
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('AdjustRest', '0003_auto_20190313_2158'),
]
operations = [
migrations.AlterField(
model_name='clientprofile',
name='email',
field=models.EmailField(max_length=254, primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='clientprofile',
name='id',
field=models.IntegerField(default=1),
),
]
Try without id = models.AutoField(primary_key=True), which is redundant as Django automatically assigns a primary key named id if you don't specify a different primary key.
If it still fails, show the code from the migration file.
I have a Post model:
class Post(models.Model):
id = models.CharField(max_length=18, primary_key=True, default=random_string)
has_upvoted = models.ManyToManyField(User, related_name="has_upvoted")
has_downvoted = models.ManyToManyField(User, related_name="has_downvoted")
and a PostScore model:
class PostScore(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
post = models.ForeignKey(Post, related_name='score')
Because I recently changed the id on my Post model from the default id = models.AutoField(primary_key=True) to the current CharField, it's causing this error when I run migrate:
django.db.utils.ProgrammingError: foreign key constraint "post_post_has_downvoted_post_id_3b2ec618_fk_post_post_id" cannot be implemented
DETAIL: Key columns "post_id" and "id" are of incompatible types: integer and character varying.
Any idea how I can fix this?
Edit:
I'm now getting this IntegrityError - I don't why as I have deleted all migration history related to the changes of id so it should just go back to normal.
Here's the traceback:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 156, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 256, in remove_field
self._remake_table(model, delete_field=field)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
self.quote_name(model._meta.db_table),
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: datatype mismatch
The thing is that when you edit PK type from AutoField to CharField it tries to get new type from old value and fail. If you want to use UUID, try using UUIDField instead.
To be honest, you should touch the id parameter at all. In case you need to associate unique string with your DB record, I'd suggest you to add new field to it
local_id = models.CharField(unique=True, mac_length=120)
If you still need your string as PK, even if it's not recommended, the most simple way is to delete the database, so it would not need to convert integer to varchar.
UPDATE #2 - Dec 13 (STILL NEED HELP)
The Github repo for the project: https://github.com/onlyandrewn/gale
From what I understand looking at my Terminal, it seems to keep referring to previous migrations after I try to ./manage.py makemigrations blog and ./manage.py migrate blog
The problem may stem from an old error for non nullable fields when I mistakenly put a string into a field called order = models.PositiveSmallIntegerField() which should have been an integer. Now, I would like to add a new field called rank, and I go through the process of ./manage.py makemigrations blog and ./manage.py migrate blog but I keep getting prompted with this in the Terminal:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
When I do run python manage.py, I get this long list of stuff in my Terminal:
Operations to perform:
Apply all migrations: blog
Running migrations:
**Applying blog.0007_auto_20141212_1622...Traceback (most recent call last):**
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/Library/Python/2.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 "/Library/Python/2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Library/Python/2.7/site-packages/django/db/backends/schema.py", line 473, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 190, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 135, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/django/db/backends/schema.py", line 99, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
**django.db.utils.IntegrityError: column order is not unique**
Two things that stand out to me in this long slew of text:
Applying blog.0007_auto_20141212_1622...Traceback (most recent call last): (The current migration is closer to 25, #7 is a very old migration)
django.db.utils.IntegrityError: column order is not unique
order = models.PositiveSmallIntegerField()
means IntegerField and NOT Text- or CharField.
and you gave "http://" which IS string.
change this to default number like 0 or 0000 whatever..
and run migrations again
"I'm wondering how to get rid of it in the Terminal." - you better wonder how to fix the problem then it goes away from your terminal
after run makemigrations django put 'http://' in your field definition, creating a wrong migration
check your migrations directory, edit the last one and manually remove 'http://' from the field definition
EDIT:
the migration 0007 create a unique consttraint on the field order but your db already contains duplicates so that migration cannot succeed.
you have a totally inconsistent situation, as your system is under development (ie not yet released) it's better that you remove all the migrations, remove the db a run makemigrations again.