Django Postgres 完整性错误
Django Postgres Integrity Error
我正在 django 中创建一些模型来表示新闻文章、它们的作者以及每篇文章的地理焦点。我需要 Article 和 Author 之间的多对多关系,以及 Article 和 Location 之间的一对多关系,其中每个位置可以有不止一篇文章,反之亦然。我尝试了各种方法,但每次 运行 在 Django 中迁移时,我都会收到以下错误:
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location"
生成table的代码如下:
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'
我认为这是相对简单的事情,但在过去的几天里我一直没有想到。如果您需要更多信息,请告诉我。
这是我现在使用的模型代码,但仍然存在与上述相同的问题:
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'
如果一篇文章可以有很多作者和一个位置,那么您需要的是与作者的多对多关系和与位置的外键。
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)
您不需要位置中的文章 ID。
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)
同样值得注意的是,您不必为模型创建 ID(它们是自动创建的),除非您想要使用除简单整数之外的其他内容作为模型 ID。
我正在 django 中创建一些模型来表示新闻文章、它们的作者以及每篇文章的地理焦点。我需要 Article 和 Author 之间的多对多关系,以及 Article 和 Location 之间的一对多关系,其中每个位置可以有不止一篇文章,反之亦然。我尝试了各种方法,但每次 运行 在 Django 中迁移时,我都会收到以下错误:
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location"
生成table的代码如下:
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'
我认为这是相对简单的事情,但在过去的几天里我一直没有想到。如果您需要更多信息,请告诉我。
这是我现在使用的模型代码,但仍然存在与上述相同的问题:
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'
如果一篇文章可以有很多作者和一个位置,那么您需要的是与作者的多对多关系和与位置的外键。
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)
您不需要位置中的文章 ID。
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)
同样值得注意的是,您不必为模型创建 ID(它们是自动创建的),除非您想要使用除简单整数之外的其他内容作为模型 ID。