0

am developing an api based on database view and am trying to a create a model for the same postgres database view with managed=False option in class meta of model, and am connecting my model via db_table parameter in the same class meta, here my database view name is "postgres_po_db_view" and am getting an error when ever am trying to run migrations for the corresponding model, please don't bother about the view or the model code, everything is good and working but my concern is when ever am trying to connect my model with my database view through class meta configuration inside the model class,when i migrated the very first time it is running smooth, and then after trying to run one more migration for another model or again trying to run the same command migrate am getting relation postgres_po_db_view already exists error...any useful lead is much appreciable..am unable to apply further migrations due to this error...

here is my model:

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)

here is my postgres data base view:

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;

  • It could help if you formatted the question text in some more readable way. A single lower-cased sentence is somewhat difficult to read. Also, it seems like googling "django relation already exists" returns meaningful results (e.g. this https://stackoverflow.com/questions/29830928/django-db-utils-programmingerror-relation-already-exists) – Dmitry Dec 02 '19 at 20:32
  • am sorry out of urgency i have typed, but i have scanned the results and most of the posts are discussing regarding db tables, i need based on a database view..that's why i posted in a separate thread – pradeep maddipatla Dec 02 '19 at 20:43
  • I am not a great Django user but I think it makes no difference to Django whether underlying "table" is view or not. – Dmitry Dec 02 '19 at 20:49

0 Answers0