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'
idseems to be present in the DB. Did you run the DB migrations?