0

I'm having trouble saving a form in a Django app. I want to create an model called 'dataset' from another model called 'image', both of which are mptt models.

models

class Image(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):  
        return self.name

class Dataset(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    image = TreeForeignKey(Image, null=True, blank=True, unique=True, related_name='image')

    def __unicode__(self):  
        return self.name

    class MPTTMeta:
        parent_attr = 'image'

When I try to save a Dataset, I get an integrity error:

IntegrityError at /associate/

column image_id is not unique

Request Method:     GET
Request URL:    http://127.0.0.1:8000/associate/
Django Version:     1.6.2
Exception Type:     IntegrityError
Exception Value:    

column image_id is not unique

Exception Location:     C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 450

views.py

def index(request):

    images = Image.objects.all()
    datasets = []
    for i in images:

        if i.rank() >= 3:

            dataset = Dataset(image=i, name=i.name)
            dataset.save()
            datasets.append(dataset)

    return render(request, 'associate/index.html', {'datasets':datasets})

def learn(request):
    if request.method == 'POST':
        try:
            dataset = request.POST.get('dataset', False)
            model = Dataset.objects.get(name=dataset)
            if model:
                print model.name
            else:
                print "no model"
        except Dataset.DoesNotExist:
            return render(request, 'associate/index.html')
        else:
            return render(request, 'associate/learn.html', {'dataset':model})
David J.
  • 1,753
  • 13
  • 47
  • 96

1 Answers1

1

You have unique=True in your Dataset model for image field. This means you cannot assign one image to different Dataset instances. But you are doing in it in your index. In your index you trying to create a new dataset for every image every time. But if the Dataset with this image already created - you will get this 'column image_id is not unique' error. Please review your application logic and either remove unique=True or rewrite the behavior.

Aldarund
  • 17,312
  • 5
  • 73
  • 104
  • I deleted 'unique=True' and ran it again and it's still coming up with the same error when I try to save the dataset model instance. It seems the model is not conforming to mptt model standards: super(MPTTModel, self).save(*args, **kwargs) – David J. Feb 18 '14 at 15:44
  • and I did delete the db and run syncdb again, btw – David J. Feb 18 '14 at 15:55
  • If u delete db and removed unique true i dont see a way u can get this error. Btw just syncdb will not work. – Aldarund Feb 18 '14 at 16:04
  • Thanks for the help, but I did delete the db and syncdb. Again, the integrity error is caught when I try to run dataset.save(). I don't understand it either. :/ – David J. Feb 18 '14 at 16:12
  • 1
    Well its perfectly understandble that the error happens why u run dataset.save with you initial code. Are u sure that with new db its same error? column image_id is not unique ? IF so - check the underslaying database column if its unique or not. It shouldnt be – Aldarund Feb 18 '14 at 19:39
  • Actually now the error says 'column name is not unique'. That's a clue as to what's going on, thanks for catching that. I'm no sure how the name is creating an integrity error though.. – David J. Feb 18 '14 at 19:43
  • 1
    Well its same error. name = models.CharField(max_length=50, unique=True) U have unique name and trying to save different datasets with the same name – Aldarund Feb 18 '14 at 22:58