[Answer]-Django Postgres Integrity Error

1👍

If an article can have many authors, and one location, then what you need is a Many-to-Many relationship with Author and Foreign Key with Location.

class Article(models.Model):
    article_id = models.AutoField(primary_key=True)
    authors = models.ManytoManyField(Author)
    location = models.ForeignKey(Location)
    article_title = models.CharField(max_length=200, unique_for_date="pub_date")
    pub_date = models.DateTimeField('date published')
    article_summary = models.TextField()
    title_id = models.CharField(max_length=200)
    section_id = models.CharField(max_length=200)

You don’t need the article Id in Location.

class Location(models.Model):
    location_id = models.AutoField(primary_key=True)
    lat = models.FloatField()
    lon = models.FloatField()
    local = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    region = models.CharField(max_length=200)
    country = models.CharField(max_length=200)
    continent = models.CharField(max_length=200)

It’s also worth noting that you don’t HAVE to create an id for your models (they’re created automatically), unless you want to use something other than simple integers for model ids.

👤onyeka

Leave a comment