Reputation: 985
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 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),
),
]
Upvotes: 1
Views: 390
Reputation: 5730
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.
Upvotes: 1