Repository: dbt-labs/dbt-core

Issue:
Issue with caching when views outside of dbt schemas select from dbt-managed schemas
## Issue

### Issue description
dbt's caching will fail hard on PG/Redshift if there exists a view _outside_ of a dbt-controlled schema that depends on a relation _inside_ of a dbt controlled schema.

### Results
```
InternalException: Cache inconsistency detected: in add_link, dependent link key _ReferenceKey(schema=u'dbt_airflow_events', identifier=u'tracking_l30d') not in cache!
```

### System information
The output of `dbt --version`:
```
0.12.0
```

### Steps to reproduce

1. Create and run an example model
```
-- models/test_it.sql

select 1 as id
```

2. Run the model
```
dbt run
```

3. Create an external dependency on this model
```
create schema debug;
create view debug.debug as (
  select * from analytics.test_it -- the model created above
)
```

4. Run dbt again
```
# this will fail
dbt run
```


Discussion:

Resolving patch:
diff --git a/dbt/adapters/postgres/impl.py b/dbt/adapters/postgres/impl.py
--- a/dbt/adapters/postgres/impl.py
+++ b/dbt/adapters/postgres/impl.py
@@ -158,7 +158,10 @@ def _link_cached_relations(self, manifest, schemas):
                                               identifier=refed_name)
             dependent = self.Relation.create(schema=dep_schema,
                                              identifier=dep_name)
-            self.cache.add_link(dependent, referenced)
+
+            # don't record in cache if this relation isn't in a relevant schema
+            if refed_schema.lower() in schemas:
+                self.cache.add_link(dependent, referenced)
 
     def _list_relations(self, schema, model_name=None):
         sql = """


Test patch:
diff --git a/test/integration/037_external_reference_test/standalone_models/my_model.sql b/test/integration/037_external_reference_test/standalone_models/my_model.sql
new file mode 100644
--- /dev/null
+++ b/test/integration/037_external_reference_test/standalone_models/my_model.sql
@@ -0,0 +1,2 @@
+
+select 1 as id
diff --git a/test/integration/037_external_reference_test/test_external_reference.py b/test/integration/037_external_reference_test/test_external_reference.py
--- a/test/integration/037_external_reference_test/test_external_reference.py
+++ b/test/integration/037_external_reference_test/test_external_reference.py
@@ -37,3 +37,39 @@ def test__postgres__external_reference(self):
         self.assertEquals(len(self.run_dbt()), 1)
         # running it again should succeed
         self.assertEquals(len(self.run_dbt()), 1)
+
+# The opposite of the test above -- check that external relations that
+# depend on a dbt model do not create issues with caching
+class TestExternalDependency(DBTIntegrationTest):
+    @property
+    def schema(self):
+        return "external_dependency_037"
+
+    @property
+    def models(self):
+        return "test/integration/037_external_reference_test/standalone_models"
+
+    def tearDown(self):
+        # This has to happen before we drop the external schema, because
+        # otherwise postgres hangs forever.
+        self._drop_schema()
+        self.adapter.drop_schema(self.external_schema, '__test')
+        super(TestExternalDependency, self).tearDown()
+
+    @use_profile('postgres')
+    def test__postgres__external_reference(self):
+        self.assertEquals(len(self.run_dbt()), 1)
+
+        # create a view outside of the dbt schema that depends on this model
+        self.external_schema = self.unique_schema()+'zz'
+        self.run_sql(
+            'create schema "{}"'.format(self.external_schema)
+        )
+        self.run_sql(
+            'create view "{}"."external" as (select * from {}.my_model)'
+            .format(self.external_schema, self.unique_schema())
+        )
+
+        # running it again should succeed
+        self.assertEquals(len(self.run_dbt()), 1)
+