0

I'm creating some models in django to represent news articles, their authors and the geographical focus for each article. I need a many-to-many relationship between Article and Author, and a one to many relationship between Article and Location, where each location can have more than one article, but not vice versa. I've tried various methods but everytime I run migrate in Django I get the following error :

django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location"

The code for generating the table is as follows:

from django.contrib.gis.db import models

class Article(models.Model):
    article_id = models.AutoField(primary_key=True)
    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)

    def __unicode__(self):
        return u'%s %s' % (self.article_title, self.pub_date)        
    class Meta:
        db_table = 'article'


class Author(models.Model):
    author_id = models.AutoField(primary_key=True)
    articles = models.ManyToManyField(Article)
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)

    def __unicode__(self):
        return u'%s %s %s' % (self.articles, self.first_name, self.last_name)   

    class Meta:
        db_table = 'author'

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)
    article = models.ForeignKey(Article, default=0)

    def __unicode__(self):
        return u'%s %s %s' % (self.location_id, self.lat, self.lon)

    class Meta:
        db_table = 'location'

I imagine it's something relatively simple but it's escaped me for the past couple of days. Let me know if you need any more info.

This is the models code I am now using but still having the same problem described above:

from django.contrib.gis.db import models

class Author(models.Model):
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)          
    class Meta:
        db_table = 'author'


class Location(models.Model):
    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)

    def __unicode__(self):
        return u'%s %s' % (self.lat, self.lon)       
    class Meta:
        db_table = 'location'


class Article(models.Model):
    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)

    def __unicode__(self):
        return u'%s %s' % (self.article_title, self.pub_date)        
    class Meta:
        db_table = 'article'
4
  • 1
    why do you assign location_id, article_id etc. ? django assign id automatically to every model Commented Apr 12, 2015 at 15:08
  • But the id seems to be present in the DB. Did you run the DB migrations? Commented Apr 12, 2015 at 15:14
  • i'm coming from an sql background and not used to not defining things explicitly! better your way though! Commented Apr 12, 2015 at 15:18
  • what do you be db migrations? i'm running migrations through django's manage.py Commented Apr 12, 2015 at 15:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great, thanks for response. I've removed the id's for each model and ran migrations on you suggested improvement but now I'm getting 'NameError: name 'Author' is not defined'. Is this something to do with order in which the tables are created? Cheers
Hi @sammy88888888, you're right. The order is crucial, if the Author model hasn't been created yet, the ForeignKey in Article must be specified like: authors = models.ManytoManyField("Author"). Or you can just move the Author model definition before the Article model definition.
So the solution above was working but i am now having the same problem again when running migrations: django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location". I have updated the models code above.
@sammy88888888 if there's no content in these tables I would suggest dropping them and running migrations again. Also, again, IF, these models are new, you could delete the files in the migrations folder. Could be that there's a conflict from when you were still manually assigning Primary Keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.