Monday, February 22, 2016

Re: key specification without a key length

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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@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>.
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/56CB7525.2040502%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment