Reputation: 478
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'
Upvotes: 0
Views: 641
Reputation: 1537
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.
Upvotes: 1