我使用Django(1.8.4)和postgress,我的makemigrations有问题。 它会随机生成新的迁移,并且这些迁移包含已在我的数据库中应用的更改。
我的模特:
class GalleryType(models.Model):
name = models.CharField(max_length=250)
description = models.TextField()
slug = models.CharField(max_length=250)
fecha_creacion = models.DateTimeField(editable=False)
fecha_modificacion = models.DateTimeField(editable=False)
active = models.BooleanField(default=True)
class GalleryExtended(models.Model):
# Link back to Photologue's Gallery model.
gallery = models.OneToOneField(Gallery, related_name='extended')
order = models.IntegerField(default=0)
type = models.ForeignKey(to='GalleryType', related_name='gallerytype', default=DEFAUTL_GALLERY_TYPE_ID)
lugar_evento = models.CharField(max_length=150, null=True, blank=True, default=None)
active = models.BooleanField(default=True)
当我运行" python manage.py makemigrations" 我用
进行迁移migrations.AlterField(
model_name='galleryextended',
name='type',
field=models.ForeignKey(default=1, to='photogallery.GalleryType', related_name='gallerytype'),
),
但是这个更改已经在我的数据库中提供了!!
CREATE TABLE photogallery_galleryextended
(
id serial NOT NULL,
"order" integer NOT NULL,
gallery_id integer NOT NULL,
type_id integer NOT NULL,
lugar_evento character varying(150),
CONSTRAINT photogallery_galleryextended_pkey PRIMARY KEY (id),
CONSTRAINT photoga_type_id_64931acb553c985b_fk_photogallery_gallerytype_id FOREIGN KEY (type_id)
REFERENCES photogallery_gallerytype (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT photogalle_gallery_id_4fb0cbc52e5218b7_fk_photologue_gallery_id FOREIGN KEY (gallery_id)
REFERENCES photologue_gallery (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT photogallery_galleryextended_gallery_id_key UNIQUE (gallery_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE photogallery_galleryextended
OWNER TO myuser;
如果我再次运行命令python manage.py makemigrations:
************************************************************************
********* 2015-10-15 17:53:04 RUN AT : local **** Site:1 *********
No changes detected
如果我再次运行它而不改变任何东西:
************************************************************************
********* 2015-10-15 17:53:18 RUN AT : local **** Site:1 *********
Migrations for 'photogallery':
0022_auto_20151015_1753.py:
- Alter field type on galleryextended
新迁移0022_auto_20151015_1753.py包含:
class Migration(migrations.Migration):
dependencies = [
('photogallery', '0021_auto_20151015_1748'),
]
operations = [
migrations.AlterField(
model_name='galleryextended',
name='type',
field=models.ForeignKey(default=1, to='photogallery.GalleryType',
related_name='gallerytype'),
),
]
如果我继续重新考虑" python manage.py makemigrations"命令我得到更多的迁移,唯一改变的是ForeignKey元素的顺序
operations = [
migrations.AlterField(
model_name='galleryextended',
name='type',
field=models.ForeignKey(to='photogallery.GalleryType', related_name='gallerytype', default=1),
),
]
operations = [
migrations.AlterField(
model_name='galleryextended',
name='type',
field=models.ForeignKey(to='photogallery.GalleryType', default=1, related_name='gallerytype'),
),
]
operations = [
migrations.AlterField(
model_name='galleryextended',
name='type',
field=models.ForeignKey(related_name='gallerytype', default=1, to='photogallery.GalleryType'),
),
]
相关:
Django 1.8 creates migration but nothing has changed
嗨,谢谢你的回复,我试过了
>>> from apps.photogallery.models import GalleryExtended
>>> GalleryExtended._meta.get_field('type').deconstruct()
('type', 'django.db.models.ForeignKey', [], {'related_name': 'gallerytype', 'default': 1, 'to': 'photogallery.GalleryType'})
>>> GalleryExtended._meta.get_field('type').deconstruct()
('type', 'django.db.models.ForeignKey', [], {'related_name': 'gallerytype', 'default': 1, 'to': 'photogallery.GalleryType'})
>>> GalleryExtended._meta.get_field('type').deconstruct()
('type', 'django.db.models.ForeignKey', [], {'related_name': 'gallerytype', 'default': 1, 'to': 'photogallery.GalleryType'})
>>> GalleryExtended._meta.get_field('type').deconstruct()
('type', 'django.db.models.ForeignKey', [], {'related_name': 'gallerytype', 'default': 1, 'to': 'photogallery.GalleryType'})
>>> from django.db.migrations.autodetector import MigrationAutodetector
>>> ma = MigrationAutodetector(None,None) ;
>>> ma.deep_deconstruct(GalleryExtended._meta.get_field('type'))
('django.db.models.ForeignKey', [], {'related_name': 'gallerytype', 'default': 1, 'to': 'photogallery.GalleryType'})
>>>
但是当我退出shell并制造数据时,它会继续产生这些不必要的错误