0

I tried to set up a second slug for my models, but this time, I got an error like:

(1062, "Duplicate entry '' for key 'slug'")

But then I faked migrations and when I tried to load the page I got into another error:

(1054, "Unknown column 'courses_faculty.slug' in 'field list'")

What can be the cause of this. I read other similar questions and I made the slug non-unique, but that's not helping..

# model
class Faculty(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=140, unique=False, default=None, null=True, blank=True)

    class Meta:
        verbose_name_plural = 'Faculties'

    def __str__(self):
        return self.name

# urls
path('courses/<slug:slug>/', views.faculties, name='faculties')

# view
def faculties(request, slug):
    query = Faculty.objects.get(slug=slug)
    context = {
        'courses': Course.objects.all(),
        'faculties': query,
        'departments': Department.objects.all(),
        'studies': StudyProgramme.objects.all(),
    }
    return render(request, 'courses/faculties.html', context)
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Alexunder
  • 25
  • 7
  • The second error is because you ran a fake migration, so the table was never created. Don't fake the migration. Fix it instead. If you have an earlier migration where slug had to be unique, you should delete it and create new migrations. – Håken Lid Feb 21 '18 at 16:51
  • Possible duplicate of [django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'slug'")](https://stackoverflow.com/questions/32383766/django-db-utils-integrityerror-1062-duplicate-entry-for-key-slug) – Håken Lid Feb 21 '18 at 16:53
  • The reason for your first error is explained in the duplicate question. – Håken Lid Feb 21 '18 at 16:53
  • Fixed it. Only solution was delete db and the recreate. – Alexunder Feb 21 '18 at 16:57

0 Answers0