I create a user model for my Django REST framework. python manage.py makemigrations
functions but when I run a migration I get:
return Database.Cursor.execute(self, query) django.db.utils.OperationalError: near "None": syntax error
Altering models.py yielded the same results if I set the User
model class to only have one id
field. I am using Django 1.11.17 and Django REST framework 3.9.0.
models.py:
class User(models.Model):
id = models.AutoField(primary_key=True)
email = models.CharField(unique=True, null=False, max_length=200)
phone = models.IntegerField(unique=True, null=False)
first_name = models.CharField(null=False, max_length=200)
last_name = models.CharField(null=False, max_length=200)
is_active = models.BooleanField(_('active'), default=False)
last_seen = models.DateTimeField(auto_now=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
avatar = models.CharField(null=True)
migrations/0001_initial.py:
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('email', models.CharField(max_length=200, unique=True)),
('phone', models.IntegerField(unique=True)),
('first_name', models.CharField(max_length=200)),
('last_name', models.CharField(max_length=200)),
('last_seen', models.DateTimeField(auto_now=True, null=True)),
('updated_at', models.DateTimeField(auto_now=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True, null=True)),
('avatar', models.CharField(null=True)),
],
),
]
Full stack trace:
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/my_username/myapp/myapp/lib/python2.7/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/my_username/myapp/myapp/lib/python2.7/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/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/my_username/myapp/myapp/lib/python2.7/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/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
schema_editor.create_model(model)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 319, in create_model
self.execute(sql, params or None)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 136, in execute
cursor.execute(sql, params)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/Users/my_username/myapp/myapp/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 326, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: near "None": syntax error
I want to successfully run the migration.
I didn't specify max_length
for the avatar
field so the query generated by Django was:
CREATE TABLE "user" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "email" varchar(200) NOT NULL UNIQUE, "phone" integer NOT NULL UNIQUE, "first_name" varchar(200) NOT NULL, "last_name" varchar(200) NOT NULL, "is_active" bool NOT NULL, "last_seen" datetime NULL, "updated_at" datetime NULL, "created_at" datetime NULL, "avatar" varchar(None) NULL);
and varchar(None)
was breaking the syntax.