old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.17 on 2020-11-18 14:35
  from __future__ import unicode_literals
  
  from django.db import migrations, connection
  
  
  def create_update_function(apps, schema_editor):
      function_sql = """
          CREATE OR REPLACE FUNCTION refresh_union_table(my_table text, my_view text) RETURNS void AS $$
          DECLARE
            schema TEXT;
            col TEXT;
            sql TEXT := '';
            column_names TEXT := '';
          BEGIN
            FOR col IN SELECT DISTINCT column_name FROM information_schema.columns
              WHERE table_name = my_table
              AND column_name NOT IN ('column_name', 'id', 'password', 'tenant', 'place')
            LOOP
            column_names := column_names || format(', %I', col);
            END LOOP;
            FOR schema IN SELECT schema_name FROM clients_client
            LOOP
              sql := sql || format('SELECT ''%I'' AS tenant', schema) || column_names || format(' FROM %I.%I UNION ALL ', schema, my_table);
            END LOOP;
            EXECUTE
              format('CREATE OR REPLACE VIEW %I AS ', my_view) || left(sql, -11);
          END
          $$ LANGUAGE plpgsql;
      """
  
      if connection.tenant.schema_name == 'public':
          schema_editor.execute(function_sql, params=None)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('clients', '0002_auto_20200205_1316'),
      ]
  
      operations = [
          migrations.RunPython(create_update_function, migrations.RunPython.noop),
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.17 on 2020-11-18 14:35
  from __future__ import unicode_literals
  
  from django.db import migrations, connection
  
  
  def create_update_function(apps, schema_editor):
      function_sql = """
          CREATE OR REPLACE FUNCTION refresh_union_table(my_table text, my_view text) RETURNS void AS $$
          DECLARE
            schema TEXT;
            col TEXT;
            sql TEXT := '';
            column_names TEXT := '';
          BEGIN
            FOR col IN SELECT DISTINCT column_name FROM information_schema.columns
              WHERE table_name = my_table
              AND column_name NOT IN ('column_name', 'password', 'tenant', 'place')
            LOOP
            column_names := column_names || format(', %I', col);
            END LOOP;
            FOR schema IN SELECT schema_name FROM clients_client
            LOOP
              sql := sql || format('SELECT ''%I'' AS tenant', schema) || column_names || format(' FROM %I.%I UNION ALL ', schema, my_table);
            END LOOP;
            EXECUTE
              format('CREATE OR REPLACE VIEW %I AS ', my_view) || left(sql, -11);
          END
          $$ LANGUAGE plpgsql;
      """
  
      if connection.tenant.schema_name == 'public':
          schema_editor.execute(function_sql, params=None)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('clients', '0002_auto_20200205_1316'),
      ]
  
      operations = [
          migrations.RunPython(create_update_function, migrations.RunPython.noop),
      ]
  
subject: Include id in union views