Inner Join ORM Django

0

I have 2 models:

class MdlCourse(models.Model):
    id = models.BigAutoField(primary_key=True)
    category = models.ForeignKey(MdlCourseCategories, on_delete=models.CASCADE)
    sortorder = models.BigIntegerField()
    fullname = models.CharField(max_length=254)

class MdlCourseCategories(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=255)

When I run, it gives the following error:

  

(1054, "Unknown column 'mdl_course.category_id' in 'field list'")

What am I doing wrong?

    
asked by anonymous 08.01.2018 / 17:33

1 answer

1

Fixed bug. As commented above, Django automatically sets the name of the Foreign Key column. It turns out that the database already exists and I'm making a system from it.

So I can not follow the Django convention. For this I had to search a lot and find that there is a parameter that is passed in the column of the Model Class so that it uses the real name of the column.

Below:

class MdlCourse(models.Model):
    id = models.BigAutoField(primary_key=True)
    category = models.ForeignKey(MdlCourseCategories, on_delete=models.CASCADE, db_column = 'category')
    sortorder = models.BigIntegerField()
    fullname = models.CharField(max_length=254)

class MdlCourseCategories(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=255)

The parameter name is db_column . And the value given to it is the actual column name.

    
10.01.2018 / 11:50