Django, Integrity error with PK, but new objects are still working fine

74 Views Asked by At

So here's my view

def add_tutorial(request):

    if request.method == 'POST':
        User = request.POST.get('user')
        # other properties
        lang = request.POST.get('lang')

        new_tutorial = Tutorial.objects.create(
            User=User, ..., lang=lang)

    context = {
        'page_title': 'create tutorial',
    }

    return render(request, "add-tutorial.html")

I do create a tutorial with forms, it all works well, I can see the tutorial where it needs to be, but for some reason I get integrity error regarding the pk

IntegrityError at /add_tutorial. UNIQUE constraint failed: main_tutorial.id

The Id in the db is correct (i use sqlight).

Before I used to specify the id in the create function, but now it shouldn't be any problem since I've dropped that db and (properly) undone all migrations; nothing changed

[the only meaningful traceback that i get is to the Tutorial.objects.create(...) line]

here's my model for the tutorial

class Tutorial(models.Model):
    '''
Tutorial is a list of lessons\n
name, is_done, length, num_of_tests, description, is_public, lang'''

    # lessons = what #! list of 'links' of lessons
    # lessons = models.
    User = models.ForeignKey(
        User, related_name='tutorial_author', on_delete=models.SET_NULL, null=True)

    name = models.CharField(max_length=100, validators=[MinLengthValidator(
        10, 'should be at least 10 characters long')])
    thumbnail = models.ImageField(
        upload_to=user_directory_path, default='def_tn.png')

    xlength = models.IntegerField(editable=False, null=True, default=0)
    num_of_tests = models.IntegerField(
        editable=False, null=True)  # somehow add a function to it
    description = models.TextField(max_length=500, null=True)
    is_public = models.BooleanField(default=False, editable=True, null=True)
    lang = models.CharField(max_length=16, choices=lang_choices, default='eng')

    urlid = models.CharField(max_length=10, null=True, default='')

    created = models.DateTimeField(
        editable=False, null=True, auto_now_add=True)
    updated = models.DateTimeField(editable=False, null=True, auto_now=True)
    ```
0

There are 0 best solutions below