key specification without a key length

637 views
Skip to first unread message

Sammi Singh

unread,
Feb 19, 2016, 4:30:46 PM2/19/16
to Django users
Hi,

I'm new to Django and facing this error "django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'id' used in key specification without a key length")"

Here is my code:

class Steps(models.Model):

    author = models.ForeignKey(User)

#    id = models.TextField(primary_key=True)

    id = models.CharField(primary_key=True, max_length = 32)

    text = models.TextField()

    status = models.TextField()

    step_id = models.TextField(null=True)

    release_id = models.TextField(null=True)

    region = models.TextField(null=True)

    # Time is a rhinocerous

    updated = models.DateTimeField(auto_now=True)

    created = models.DateTimeField(auto_now_add=True)


class UserConfig(models.Model):

    author = models.ForeignKey(User)

#    id = models.TextField(primary_key=True)

    id = models.CharField(primary_key=True, max_length = 32)

    co_range = models.TextField()

    tu_range = models.TextField()

    st_range = models.TextField()

    de_host = models.TextField()

    in_host1 = models.TextField()

    in_host2 = models.TextField()

    in_host3 = models.TextField()

    co_host = models.TextField()

    vp_name = models.TextField()


    # Time is a rhinocerous

    updated = models.DateTimeField(auto_now=True)

    created = models.DateTimeField(auto_now_add=True)


Any help would be appreciated......


Regards

Sammi

Mike Dewhirst

unread,
Feb 19, 2016, 6:26:55 PM2/19/16
to django...@googlegroups.com
On 20/02/2016 6:26 AM, Sammi Singh wrote:
> Hi,
>
> I'm new to Django and facing this error
> "*/django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'id' used
> in key specification without a key length")/*"
>
> Here is my code:
>
> class Steps(models.Model):
>
> Â Â author = models.ForeignKey(User)
>
> #Â Â id = models.TextField(primary_key=True)

I don't think it makes sense to use a text field as a primary key. If
you don't want to use the default primary key (and you need a strong
case to want something else) and you really want a string as a primary
key use models.CharField(max_length=99, primary_key=True)


>
> Â Â id = models.CharField(primary_key=True, max_length = 32)
>
> Â Â text = models.TextField()
>
> Â Â status = models.TextField()
>
> Â Â step_id = models.TextField(null=True)
>
> Â Â release_id = models.TextField(null=True)
>
> Â Â region = models.TextField(null=True)
>
> Â Â # Time is a rhinocerous
>
> Â Â updated = models.DateTimeField(auto_now=True)
>
> Â Â created = models.DateTimeField(auto_now_add=True)
>
>
> class UserConfig(models.Model):
>
> Â Â author = models.ForeignKey(User)
>
> #Â Â id = models.TextField(primary_key=True)
>
> Â Â id = models.CharField(primary_key=True, max_length = 32)
>
> Â Â co_range = models.TextField()
>
> Â Â tu_range = models.TextField()
>
> Â Â st_range = models.TextField()
>
> Â Â de_host = models.TextField()
>
> Â Â in_host1 = models.TextField()
>
> Â Â in_host2 = models.TextField()
>
> Â Â in_host3 = models.TextField()
>
> Â Â co_host = models.TextField()
>
> Â Â vp_name = models.TextField()
>
>
> Â Â # Time is a rhinocerous
>
> Â Â updated = models.DateTimeField(auto_now=True)
>
> Â Â Â created = models.DateTimeField(auto_now_add=True)
>
>
> Any help would be appreciated......
>
>
> Regards
>
> Sammi
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5100d863-f887-4b97-9ea4-95227ab99bdf%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/5100d863-f887-4b97-9ea4-95227ab99bdf%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Luis Zárate

unread,
Feb 19, 2016, 6:39:54 PM2/19/16
to django...@googlegroups.com
I response in the other mail, but I am curious
changing TextField for. CharField with max_length has big impact in the database performance.

I saw you have a lot of TextFields so are you sure that you need all those TextFields ? Or y
could you change those TextFields for CharFields with a small max_length ?

#    id = models.TextField(primary_key=True)


    id = models.CharField(primary_key=True, max_length = 32)




> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56C7A494.4060103%40dewhirst.com.au.

> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Sammi Singh

unread,
Feb 22, 2016, 2:37:13 PM2/22/16
to Django users
Thanks Mike,

I'm using models and I made the changes like you suggested but I'm still getting the same error....

id = models.CharField(max_length=99, primary_key=True)


python manage.py makemigrations talk

Migrations for 'talk':

  0008_auto_20160222_1925.py:

    - Remove field ids from userconfig

    - Add field id to userconfig

    - Alter field id on steps


python manage.py migrate talk

Operations to perform:

  Apply all migrations: talk

Running migrations:

  Rendering model states... DONE

  Applying talk.0003_steps...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 353, in execute_from_command_line

    utility.execute()

  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 345, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv

    self.execute(*args, **cmd_options)

  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 399, in execute

    output = self.handle(*args, **options)

  File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle

    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)

  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate

    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)

  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards

    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration

    state = migration.apply(state, schema_editor)

  File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 123, in apply

    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)

  File "/Library/Python/2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards

    schema_editor.create_model(model)

  File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 284, in create_model

    self.execute(sql, params or None)

  File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute

    cursor.execute(sql, params)

  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 79, in execute

    return super(CursorDebugWrapper, self).execute(sql, params)

  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 64, in execute

    return self.cursor.execute(sql, params)

  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95, in __exit__

    six.reraise(dj_exc_type, dj_exc_value, traceback)

  File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 62, in execute

    return self.cursor.execute(sql)

  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute

    return self.cursor.execute(query, args)

  File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute

    self.errorhandler(self, exc, value)

  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler

    raise errorclass, errorvalue

django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'id' used in key specification without a key length")

Mike Dewhirst

unread,
Feb 22, 2016, 3:53:33 PM2/22/16
to django...@googlegroups.com
On 23/02/2016 6:37 AM, Sammi Singh wrote:
> Thanks Mike,
>
> I'm using models and I made the changes like you suggested but I'm still
> getting the same error....
>
> *id = models.CharField(max_length=99, primary_key=True)*

That was a stab in the dark trying to indicate to you that a text field
is inappropriate as a key field.

I also indicated that it is unusual to even use a char field as primary
key. You would want to be certain such a path is necessary. It is also
highly unusual to want a primary key to carry specific business data
such as might be your intention.

Before expending effort on fixing the problem*, can you explain why you
want to control the primary key yourself instead of giving that task to
Django's built-in primary key?

* The last line of the traceback indicates the migration failed then
naturally rolled the transaction back and left your database with an
unchanged text (blob) field as primary key. Why it failed I don't know
but there is probably an easier solution.

Mike

>
>
> /python manage.py makemigrations talk/
>
> *Migrations for 'talk':*
>
> Â *0008_auto_20160222_1925.py*:
>
> Â Â - Remove field ids from userconfig
>
> Â Â - Add field id to userconfig
>
> Â Â - Alter field id on steps
>
>
> /python manage.py migrate talk/
>
> *Operations to perform:*
>
> *Â Apply all migrations: *talk
>
> *Running migrations:*
>
> Â Rendering model states...*DONE*
>
> Â Applying talk.0003_steps...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 353, in execute_from_command_line
>
> Â Â utility.execute()
>
> Â File
> "/Library/Python/2.7/site-packages/django/core/management/__init__.py",
> line 345, in execute
>
> Â Â self.fetch_command(subcommand).run_from_argv(self.argv)
>
> Â File
> "/Library/Python/2.7/site-packages/django/core/management/base.py", line
> 348, in run_from_argv
>
> Â Â self.execute(*args, **cmd_options)
>
> Â File
> "/Library/Python/2.7/site-packages/django/core/management/base.py", line
> 399, in execute
>
> Â Â output = self.handle(*args, **options)
>
> Â File
> "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py",
> line 200, in handle
>
> Â Â executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/migrations/executor.py",
> line 92, in migrate
>
> Â Â self._migrate_all_forwards(plan, full_plan, fake=fake,
> fake_initial=fake_initial)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/migrations/executor.py",
> line 121, in _migrate_all_forwards
>
> Â Â state = self.apply_migration(state, migration, fake=fake,
> fake_initial=fake_initial)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/migrations/executor.py",
> line 198, in apply_migration
>
> Â Â state = migration.apply(state, schema_editor)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/migrations/migration.py",
> line 123, in apply
>
> Â Â operation.database_forwards(self.app_label, schema_editor,
> old_state, project_state)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/migrations/operations/models.py",
> line 59, in database_forwards
>
> Â Â schema_editor.create_model(model)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py",
> line 284, in create_model
>
> Â Â self.execute(sql, params or None)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py",
> line 110, in execute
>
> Â Â cursor.execute(sql, params)
>
> Â File "/Library/Python/2.7/site-packages/django/db/backends/utils.py",
> line 79, in execute
>
> Â Â return super(CursorDebugWrapper, self).execute(sql, params)
>
> Â File "/Library/Python/2.7/site-packages/django/db/backends/utils.py",
> line 64, in execute
>
> Â Â return self.cursor.execute(sql, params)
>
> Â File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95,
> in __exit__
>
> Â Â six.reraise(dj_exc_type, dj_exc_value, traceback)
>
> Â File "/Library/Python/2.7/site-packages/django/db/backends/utils.py",
> line 62, in execute
>
> Â Â return self.cursor.execute(sql)
>
> Â File
> "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py",
> line 112, in execute
>
> Â Â return self.cursor.execute(query, args)
>
> Â File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line
> 205, in execute
>
> Â Â self.errorhandler(self, exc, value)
>
> Â File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line
> 36, in defaulterrorhandler
>
> Â Â raise errorclass, errorvalue
>
> django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'id' used in
> key specification without a key length")
>
>
>
> On Friday, February 19, 2016 at 1:30:46 PM UTC-8, Sammi Singh wrote:
>
> Hi,
>
> I'm new to Django and facing this error
> "*/django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'id'
> used in key specification without a key length")/*"
>
> Here is my code:
>
> class Steps(models.Model):
>
> Â Â author = models.ForeignKey(User)
>
> #Â Â id = models.TextField(primary_key=True)
>
> Â Â id = models.CharField(primary_key=True, max_length = 32)
>
> Â Â text = models.TextField()
>
> Â Â status = models.TextField()
>
> Â Â step_id = models.TextField(null=True)
>
> Â Â release_id = models.TextField(null=True)
>
> Â Â region = models.TextField(null=True)
>
> Â Â # Time is a rhinocerous
>
> Â Â updated = models.DateTimeField(auto_now=True)
>
> Â Â created = models.DateTimeField(auto_now_add=True)
>
>
> class UserConfig(models.Model):
>
> Â Â author = models.ForeignKey(User)
>
> #Â Â id = models.TextField(primary_key=True)
>
> Â Â id = models.CharField(primary_key=True, max_length = 32)
>
> Â Â co_range = models.TextField()
>
> Â Â tu_range = models.TextField()
>
> Â Â st_range = models.TextField()
>
> Â Â de_host = models.TextField()
>
> Â Â in_host1 = models.TextField()
>
> Â Â in_host2 = models.TextField()
>
> Â Â in_host3 = models.TextField()
>
> Â Â co_host = models.TextField()
>
> Â Â vp_name = models.TextField()
>
>
> Â Â # Time is a rhinocerous
>
> Â Â updated = models.DateTimeField(auto_now=True)
>
> Â Â Â created = models.DateTimeField(auto_now_add=True)
>
>
> Any help would be appreciated......
>
>
> Regards
>
> Sammi
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/7dd0d105-be6b-419d-bf50-f1eb26ef0a27%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/7dd0d105-be6b-419d-bf50-f1eb26ef0a27%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages