Problem when deleting a register from the database
Hi all! I have a delete that isn't working properly. It deletes more than 1 register when it shouldn't because I'm specifying the primary key in the filter. The class file is this: class UserInstrument(models.Model): user_name = models.CharField(primary_key=True, maxlength=60) instrument_name = models.CharField(blank=False, maxlength=60) permission = models.TextField(blank=True) def __str__(self): string="user_name: "+self.user_name+"; instr_name: "+self.instrument_name+"; permission: "+self.permission return string class Meta: db_table = 'User_instrument' unique_together = (("user_name", "instrument_name"),) In the view function I'm doing this: UserInstrument.objects.filter(user_name__exact=username, instrument_name__exact=itemid).delete() If I do this: number=UserInstrument.objects.filter(user_name__exact=username, instrument_name__exact=itemid).count() assert False In the error page number=1 which means that there is only one register with that user_name and that instrument_name. I don't understand why this is happening. It should delete only one register and instead it's deleting all records with the user_name=username. I went directly to MySQL and did select * from User_instrument where user_name='abc' and instrument_name='ABC'; these were the user_name and instrument_name which were being used, and only one register came out. Oh and another detail, in the MySQL table, the primary key is the user_name and the instrument_name together. Does anyone know why this is happening?? Is there a way in which I can see the SQL commands Django is sending to MySQL? Thanks for your time. Ana --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Problem when deleting a register from the database
AnaReis skrev: > Hi all! > I have a delete that isn't working properly. It deletes more than 1 > register when it shouldn't because I'm specifying the primary key in > the filter. > The class file is this: > class UserInstrument(models.Model): > user_name = models.CharField(primary_key=True, maxlength=60) > instrument_name = models.CharField(blank=False, maxlength=60) > permission = models.TextField(blank=True) > def __str__(self): > string="user_name: "+self.user_name+"; instr_name: > "+self.instrument_name+"; permission: "+self.permission > return string > class Meta: > db_table = 'User_instrument' > unique_together = (("user_name", "instrument_name"),) > In the view function I'm doing this: > UserInstrument.objects.filter(user_name__exact=username, > instrument_name__exact=itemid).delete() > > Oh and another detail, in the MySQL table, the primary key is the > user_name and the instrument_name together. > I am not sure whether this is the cause of your problem, but - Django doesn't support multi-field primary keys - Your model class tells Django that "user_name" alone is the primary key, which is wrong (and will lead to strange primary key lookups). - Django implements its own cascading delete, independent of the database. In this I believe it loops throught the queryset, and deletes the objects based on the primary key. Since you "lied" to it about what the PK is, it deletes the wrong objects. Quick solution: Remove the primary_key=True from the model - this will add the standard "id" autonumber field. Slow solution: Add support for multi-field primary keys to the database. Nis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Problem when deleting a register from the database
On Jul 26, 4:24 pm, Nis Jørgensen <[EMAIL PROTECTED]> wrote: > AnaReis skrev: > > > Hi all! > > I have a delete that isn't working properly. It deletes more than 1 > > register when it shouldn't because I'm specifying the primary key in > > the filter. > > The class file is this: > > class UserInstrument(models.Model): > > user_name = models.CharField(primary_key=True, maxlength=60) > > instrument_name = models.CharField(blank=False, maxlength=60) > > permission = models.TextField(blank=True) > > def __str__(self): > > string="user_name: "+self.user_name+"; instr_name: > > "+self.instrument_name+"; permission: "+self.permission > > return string > > class Meta: > > db_table = 'User_instrument' > > unique_together = (("user_name", "instrument_name"),) > > In the view function I'm doing this: > > UserInstrument.objects.filter(user_name__exact=username, > > instrument_name__exact=itemid).delete() > > > Oh and another detail, in the MySQL table, the primary key is the > > user_name and the instrument_name together. > > I am not sure whether this is the cause of your problem, but > > - Django doesn't support multi-field primary keys > - Your model class tells Django that "user_name" alone is the primary > key, which is wrong (and will lead to strange primary key lookups). > - Django implements its own cascading delete, independent of the > database. In this I believe it loops throught the queryset, and deletes > the objects based on the primary key. Since you "lied" to it about what > the PK is, it deletes the wrong objects. > > Quick solution: Remove the primary_key=True from the model - this will > add the standard "id" autonumber field. > > Slow solution: Add support for multi-field primary keys to the database. > > Nis Hi, It would be much easier to just put an autonumber field, the problem is that I can't change the database. In the database, the primary key is a multi field primary key, it's the user_name and the instrument_name, and unfortunately I can't change this. The way I found to fix this was to put primary_key=True in all the fields that are actually primary keys. Now it works properly. Thanks for your help! Ana --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---