2

I have the following model

class Staff(models.Model):
    username = models.OneToOneField(Person, primary_key=True)
    room = models.CharField(blank=True, max_length=12)

When I run the following code I get the error Truncated incorrect DOUBLE value: 'lacc1'

s = Staff.objects.get(pk = 'lacc1').delete()

I assume this has something to do with the primary key being a string. Does anyone know how I can solve this problem.

It is only on deletes. If I just want to get the object using get or filter it works fine. There is also a object in the db with pk lacc1 so that is not the problem.

EDIT

Person Class

class Person(models.Model):
    username = models.CharField(primary_key=True, max_length=12)
    title = models.CharField(max_length=25)
    forenames = models.CharField(max_length=50)
    surname =  models.CharField(max_length=50)
Spacedman
  • 92,590
  • 12
  • 140
  • 224
John
  • 21,047
  • 43
  • 114
  • 155

1 Answers1

3

Your primary key is not a string, it's an int.

username = models.OneToOneField(Person, primary_key=True)

username here is not a CharField but a OneToOneField, meaning a relation to a Person object model, meaning a field containing the id of the row of this person instance in the person table.

You may want to rename it this way:

person = models.OneToOneField(Person, primary_key=True)

And, assuming your Person object has a usernameattribute, you can delete your staff this way:

s = Staff.objects.get(person__username='lacc1').delete()

However, you must know that this implies a JOIN on the person_id field and a filter on a the Staff username field, which is probably not indexed. It will be slower than what you expected, but I doubt it should ba any trouble.

Bite code
  • 578,959
  • 113
  • 301
  • 329
  • 1
    Thanks for the answer but it didn't work. I have no problem getting the objects, its just when I try to call the delete() method. So s = Staff.objects.get(person__username='lacc1') would give me the object but as soon as I call the delete method on the object I get the error. – John Feb 24 '11 at 14:31
  • Delete you database, rerun syncdb then create your objects using the django admin. You should not have a Staff object with a primary key being 'lacc1'. I can't guess what you made to create it, but it does not match your model. – Bite code Feb 24 '11 at 14:59