Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

Why make this change?

Closes #2780

DAB currently supports relationships only between table entities. This extends relationship support to views, enabling:

  • view → view
  • view → table
  • table → view

What is this change?

Extends relationship metadata processing and join predicate generation to work with view-backed entities.

Key changes:

  • RuntimeConfigValidator.cs: Allow relationships for view entities when database-type is mssql or dwsql. Stored procedures remain unsupported.
  • SqlMetadataProvider.cs:
    • ProcessRelationships now accepts DatabaseObject instead of DatabaseTable
    • Creates DatabaseView or DatabaseTable based on target entity's source.type
    • Converts to DatabaseTable internally for RelationShipPair serialization compatibility
  • ISqlMetadataProvider.cs: Updated VerifyForeignKeyExistsInDB signature to accept DatabaseObject

Design notes:

  • RelationShipPair keeps DatabaseTable properties for serialization. Works because DatabaseObject.Equals compares only schema+name, not type.
  • Linking objects in M:N relationships must remain tables per requirement.
  • Multi-create mutations on views are out of scope.

Config example (view → table):

"BooksView": {
  "source": { "object": "dbo.vw_books_details", "type": "view" },
  "relationships": {
    "publisher": {
      "cardinality": "one",
      "target.entity": "Publisher",
      "relationship.fields": [ "publisher_id:id" ]
    }
  }
}

How was this tested?

  • Integration Tests
  • Unit Tests

208 config validation tests, 72 schema converter tests, 18 serialization tests pass. CodeQL: no alerts.

Sample Request(s)

GraphQL query traversing view → table relationship:

{
  booksViews {
    items {
      title
      publisher {  # Relationship field to table entity
        name
      }
    }
  }
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • management.azure.com
    • Triggering command: /usr/bin/pwsh pwsh -NoProfile -NonInteractive -EncodedCommand DQAKACQARQByAHIAbwByAEEAYwB0AGkAbwBuAFAAcgBlAGYAZQByAGUAbgBjAGUAIAA9ACAAJwBTAHQAbwBwACcADQAKAFsAdgBlAHIAcwBpAG8AbgBdACQAbQBpAG4AaQBtAHUAbQBWAGUAcgBzAGkAbwBuACAAPQAgACcAMgAuADIALgAwACcADQAKAA0ACgAkAG0AIAA9ACAASQBtAHAAbwByAHQALQBNAG8AZAB1AGwAZQAgAEEAegAuAEE (dns block)
  • test
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/data-api-builder/data-api-builder/src/out/tests/net8.0/Azure.DataApiBuilder.Service.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/data-api-builder/data-api-builder/src/out/tests/net8.0/Azure.DataApiBuilder.Service.Tests.deps.json /home/REDACTED/work/data-api-builder/data-api-builder/src/out/tests/net8.0/testhost.dll --port 45391 --endpoint 127.0.0.1:045391 --role client --parentprocessid 6030 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[Enh]: Add relationship support for entities of object type view.</issue_title>
<issue_description>## What?

Extend relationship support so entities backed by views can participate in relationships:

  • view → view
  • view → table
  • table → view

No new config syntax. Uses existing entities.<name>.relationships settings (including relationship.fields and optional linking.* for M:N).

Why?

Today DAB supports relationships between tables entities, but we do not support relationships where either side is a view.

Background

Even though views often lack PK/FK constraints in the database, DAB already allows relationships to be defined in config without requiring DB constraints. If we can build JOIN predicates from configured relationship fields, we should be able to do the same when the underlying objects are views.

Motivation

This unlocks common patterns where views are the preferred read model.

How?

Scope

  • Support relationship JOIN generation for Read operations when either source or target entity is source.type: "view".
  • Relationships must be fully defined in config (no reliance on DB FK constraints).
  • This enhancement is only supported when data-source.database-type is mssql or dwsql . Basically, SQL Server.

Requirements

  1. Entity types allowed

    • If Entity.Source.Type is table or view, it may be used in relationships.
    • Stored procedures remain out of scope.
  2. Join predicate source

    • Use relationship.fields (sourceFields ↔ targetFields) to build join predicates.
    • Do not require PK/FK constraints to exist in the database.
  3. Nullability

    • Don’t assume FK nullability based on DB constraints for views. Use existing behavior, but avoid throwing solely because metadata lacks constraints.
  4. M:N relationships

    • Linking object must remain a table (practical requirement for join + existing behavior).
    • Allow source/target to be views, but linking object stays table.
  5. Unsupported

    • Multi-create mutations against view-backed entities are not part of this enhancement. We can tackle this later if we see customer demand, but this would EXPLODE the scope and complexity of this feature right now.

Config example (view → table)

"entities": {
  "BooksView": {
    "source": { "object": "dbo.vw_books_details", "type": "view" },
    "permissions": [ { "role": "anonymous", "actions": [ "*" ] } ],
    "relationships": {
      "publisher": {
        "cardinality": "one",
        "target.entity": "Publisher",
        "relationship.fields": [ "publisher_id:id" ]
      }
    }
  }
}

Config example (view → view)

"relationships": {
  "authors": {
    "cardinality": "many",
    "target.entity": "AuthorsView",
    "relationship.fields": [ "id:book_id" ]
  }
}

Notes

  • This feature is “just” about: read relationship config → build JOIN predicate even when underlying objects are views.
  • Existing relationship schema stays the same.
  • This feature only supports SQL Server/TSQL (mssql or dwsql).

Implementation

Key code paths to update (these are the likely choke points for “tables only” assumptions):

  • Relationship metadata hydration / FK definition creation:

    • src/Core/Services/MetadataProviders/SqlMetadataProvider.cs
      • currently builds relationship metadata and FK definitions; ensure it can represent relationships when DatabaseObject is a view (or at least not reject it).
  • SQL join predicate construction used by query builders:

    • src/Core/Resolvers/Sql Query Structures/BaseSqlQueryStructure.cs
      • AddJoinPredicatesForRelationship(...) and related helpers should work when either side is a view-backed DatabaseObject.
  • GraphQL schema relationship field generation (mostly type-level, but ensure no “table-only” validation trips):

    • src/Service.GraphQLBuilder/Sql/SchemaConverter.cs (GenerateFieldForRelationship(...))

Core logic

https://github.com/Azure/data-api-builder/search?q=GetType%28%29+%21%3D+typeof%28DatabaseTable%29&type=code</issue_description>

<agent_instructions>Introduce as little code as possible. Our goal is to make it so views can relate like tables can today. As simple as possible with, if possible, zero change to the user experience or configuration structure. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 2, 2026 16:52
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
…pport

Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Add relationship support for entities of object type view Add relationship support for entities of object type view Feb 2, 2026
Copilot AI requested a review from JerryNixon February 2, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enh]: Add relationship support for entities of object type view.

2 participants