Skip to content

Applying view gives type mismatch error, but the view works #3608

@chrispytoes

Description

@chrispytoes

I have a view that Atlas is refusing to apply because of the error: pq: operator does not exist: text = integer. The view code runs fine on its own, and my column definitions all match the query output exactly. In-fact, I generated this view object from the atlas inspect command.

view "datto" "v_devices" {
  schema = schema.datto
  column "id" {
    null = true
    type = text
  }
  column "company_id" {
    null = true
    type = integer
  }
  column "name" {
    null = true
    type = text
  }
  column "model" {
    null = true
    type = text
  }
  column "hidden" {
    null = true
    type = boolean
  }
  column "service_plan" {
    null = true
    type = text
  }
  column "local_storage_used" {
    null = true
    type = bigint
  }
  column "local_storage_available" {
    null = true
    type = bigint
  }
  column "offsite_storage_used" {
    null = true
    type = bigint
  }
  column "last_seen_date" {
    null = true
    type = timestamptz
  }
  column "registration_date" {
    null = true
    type = timestamptz
  }
  column "service_period" {
    null = true
    type = timestamptz
  }
  column "warranty_expire" {
    null = true
    type = timestamptz
  }
  column "agent_count" {
    null = true
    type = integer
  }
  column "share_count" {
    null = true
    type = integer
  }
  column "alert_count" {
    null = true
    type = integer
  }
  column "uptime" {
    null = true
    type = bigint
  }
  column "company_name" {
    null = true
    type = text
  }
  column "addition_id" {
    null = true
    type = integer
  }
  column "mrr" {
    null = true
    type = numeric
  }
  column "quantity" {
    null = true
    type = numeric
  }
  column "billed_quantity" {
    null = true
    type = numeric
  }
  column "unit_price" {
    null = true
    type = numeric
  }
  column "ext_price" {
    null = true
    type = numeric
  }
  column "unit_cost" {
    null = true
    type = numeric
  }
  column "ext_cost" {
    null = true
    type = numeric
  }
  column "audit_quantity" {
    null = true
    type = integer
  }
  column "audit_billed_quantity" {
    null = true
    type = integer
  }
  column "local_storage_percent" {
    null = true
    type = numeric
  }
  column "missing_serial" {
    null = true
    type = boolean
  }
  as = <<-SQL
    WITH device_counts AS (
      SELECT
        d_1.company_id,
        count(*) AS device_count
      FROM datto.devices d_1
      WHERE d_1.hidden = false AND d_1.last_seen_date > (now() - '2 years'::interval)
      GROUP BY d_1.company_id
    )
    SELECT
      d.id,
      d.company_id,
      d.name,
      d.model,
      d.hidden,
      d.service_plan,
      d.local_storage_used,
      d.local_storage_available,
      d.offsite_storage_used,
      d.last_seen_date,
      d.registration_date,
      d.service_period,
      d.warranty_expire,
      d.agent_count,
      d.share_count,
      d.alert_count,
      d.uptime,
      c.name AS company_name,
      a.id AS addition_id,
      a.mrr,
      a.quantity,
      a.billed_quantity,
      a.unit_price,
      a.ext_price,
      a.unit_cost,
      a.ext_cost,
      d.agent_count AS audit_quantity,
      CASE
        WHEN d.service_plan ~~* '%Per Agent%'::text THEN d.agent_count
        ELSE 1
      END AS audit_billed_quantity,
      CASE
        WHEN d.local_storage_available = 0 THEN 0::numeric
        ELSE round(d.local_storage_used::numeric / (d.local_storage_used::numeric + d.local_storage_available::numeric) * 100::numeric, 2)::numeric(12,2)
      END AS local_storage_percent,
      CASE
        WHEN dc.device_count = 1 THEN false
        WHEN a.id IS NULL THEN true
        ELSE false
      END AS missing_serial
    FROM datto.devices d
    JOIN public.companies c ON c.id = d.company_id
    JOIN device_counts dc ON dc.company_id = d.company_id
    LEFT JOIN LATERAL (
      SELECT
        a_1.id,
        a_1.quantity,
        a_1.billed_quantity,
        a_1.hours_quantity,
        a_1.hours_billed_quantity,
        a_1.less_included,
        a_1.subtract_included,
        a_1.unit_price,
        a_1.unit_cost,
        a_1.eff_billed_qty_non_hours,
        a_1.ext_price,
        a_1.ext_cost,
        a_1.bill_customer,
        a_1.billable,
        a_1.effective_date,
        a_1.serial_number,
        a_1.active,
        a_1.issue_exception,
        a_1.catalog_item_id,
        a_1.catalog_item_name,
        a_1.catalog_item_agreement_type_id,
        a_1.catalog_item_qty_is_agrmt_hours,
        a_1.catalog_item_subtract_included,
        a_1.catalog_item_agreement_type_name,
        a_1.catalog_item_wrong_agreement_type,
        a_1.catalog_item_not_approved,
        a_1.company_id,
        a_1.company_name,
        a_1.agreement_id,
        a_1.agreement_name,
        a_1.agreement_type_id,
        a_1.agreement_type_name,
        a_1.mrr,
        a_1.cost,
        a_1.margin,
        a_1.margin_pct,
        a_1.services
      FROM public.v_additions_simple a_1
      WHERE dc.device_count = 1 AND a_1.company_id = d.company_id AND a_1.catalog_item_id = 8509 OR dc.device_count > 1 AND a_1.serial_number = d.id
      LIMIT 1
    ) a ON true
    WHERE d.last_seen_date > (now() - '2 years'::interval);
  SQL
  depends_on = [table.companies, table.datto.devices, view.v_additions_simple]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions