django.db.utils.ProgrammingError:关系“ postgres_po_db_view”已存在

时间:2019-12-02 18:58:39

标签: django database postgresql django-rest-framework database-view

我基于数据库视图开发一个api,并尝试在模型的类meta中使用managed = False选项为同一postgres数据库视图创建模型,并通过db_table参数在同一类meta中连接我的模型,在这里,我的数据库视图名称是“ postgres_po_db_view”,并且在尝试为相应模型运行迁移时遇到错误,请不要理会该视图或模型代码,一切都很好并且可以正常工作,但是我关心的是何时我试图通过模型类中的类元配置将模型与数据库视图连接起来,当我第一次迁移时,它运行得很顺利,然后尝试为另一个模型再进行一次迁移或再次尝试运行相同的模型时命令Migration正在获取关系postgres_po_db_view已经存在错误...任何有用的线索都非常明显..am由于此错误而无法应用进一步的迁移...

这是我的模特:

class ProductionOrderView(models.Model):
    class Meta:
        managed  = False,
        ordering = '-creation_time',
        db_table = 'postgres_po_db_view'

    DRAFT = 'draft'
    PLANNING = 'planning'
    NOT_STARTED = 'not_started'
    IN_PROGRESS = 'in_progress'
    CANCELLED = 'cancelled'
    DONE = 'done'
    FAILED = 'failed'
    STATUS_CHOICES = (
        (DRAFT, 'Draft'),
        (PLANNING, 'Planning'),
        (NOT_STARTED, 'Not Started'),
        (IN_PROGRESS, 'In Progress'),
        (CANCELLED, 'Cancelled'),
        (DONE, 'Done'),
        (FAILED, 'Failed'),
    )
    ACTIVE_STATUS_LIST = [DRAFT, IN_PROGRESS, PLANNING]
    id = models.UUIDField(
        default=uuid.uuid4,
        primary_key=True,
        editable=False,
        unique=True,
    )
    name = models.CharField(
        max_length=64,
        default = '',
        blank = True,
    )
    deadline = models.DateTimeField(**NB)
    planned_date = models.DateTimeField(**NB)
    print_date = models.DateTimeField(**NB)
    build = models.CharField(
        max_length=256,
        default='',
        blank=True,
    )
    sop = models.CharField(
        max_length=128,
        default='',
        blank=True,
    )
    notes = models.CharField(
        max_length=256,
        default='',
        blank=True,
    )
    build_qty = models.IntegerField(default=0)
    status = models.CharField(
        max_length=64,
        default='',
    )
    last_updated_by =models.CharField(
        max_length=64,
        default='',
    )
    site = JSONField(
        default=dict
    )
    site_id = models.CharField(
        max_length=64,
        default='',
    )
    production_type = models.CharField(
        max_length=64,
        default='',
        blank=True,
    )
    operation_failures = JSONField(
        default=dict
    )
    operation_status = JSONField(
        default=dict
    )
    files = JSONField(
        default=dict
    )
    sap_backflush_submission_status = models.BooleanField(default=False)
    creation_time = models.DateTimeField(**NB)
    update_time = models.DateTimeField(**NB)

这是我的postgres数据库视图:

create or replace view postgres_po_db_view as

(select po.id,po.name as name),
(po.institution_id as institution),
(po.deadline),
(po.planned_date),
(po.print_date),
(po.status),
(po.production_type),
(po.notes),
(po.creation_time),
(po.update_time),
(Select bu.name from skyforge_build as bu where bu.id = po.build_id) as build,
(Select so.name from skyforge_sop as so where so.id = po.sop_id) as sop,
(select json_agg(site) from (Select si.name,si.id from skyforge_site as si where si.id=po.site_id) as site) as site,
(Select us.first_name from auth_user as us where us.id=po.last_updated_by_id) as last_updated_by,
(Select sum(quantity) from skyforge_buildpart as bup where bup.build_id=po.build_id) as build_qty,
(select json_agg(totrecs) as operation_fail_list from (select operation_id,array_agg(id) as operation_failures from skyforge_failure as fail where ROW(operation_id) in (select id from skyforge_operation as op where op.production_order_id = po.id) group by fail.operation_id) as totrecs) as operation_failures,
(select json_agg(json_build_object(op.id,op.status)) from skyforge_operation as op where op.production_order_id = po.id) as operation_status,
(select json_agg(vtorecs) from (select id,name,content from skyforge_file group by id having parent_id=po.id union select id,name,content from (select fi.id,fi.name,fi.content,po.id as poid from skyforge_file as fi,skyforge_productionorder as po where fi.id in (po.backflush_id,po.pdf_id,po.inspection_template_full_id,po.inspection_template_sampling_id,po.production_slips_id,po.serialized_part_tags_id,po.batches_qr_labels_id)) as recs where recs.poid=po.id) as vtorecs) as files,
(po.sap_backflush_submission_status)

From skyforge_productionorder as po;

0 个答案:

没有答案