Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for foreign key specification. #489

Closed
julianbergerkoerber opened this issue Oct 29, 2024 · 5 comments
Closed

Add support for foreign key specification. #489

julianbergerkoerber opened this issue Oct 29, 2024 · 5 comments

Comments

@julianbergerkoerber
Copy link

The Databricks SQL dialect offers the possibility to add a foreign key reference in the CREATE TABLE statement. I would like to use this with the datacontract.yml, but currently there is no possibility to do this.

For example, I would like to add the foreignKey as a setting for the fields settings, like this:

foreignKey:
  catalog: default
  schema: default
  table: producers
  column: producer

Is this possible, or are there any problems with this?
I also added an example on the desired output for my issue:

Example Contract

dataContractSpecification: 0.9.3
id: my-data-contract-id
info:
  title: My Data Contract
  version: 0.0.1
servers:
  production:
    type: databricks
    catalog: default
    schema: default
models:
  cars:
    description: The Cars table contains cars.
    type: table
    fields:
      producer:
        description: Producer of the car.
        type: string
        required: true
        primary: false
        foreignKey:
          catalog: default
          schema: default
          table: producers
          column: producer
  producers:
    description: The Procuders table contains car producer.
    type: table
    fields:
      producer:
        description: A Car Producer.
        type: string
        required: true
        primary: true

Output:

-- Data Contract: my-data-contract-id
-- SQL Dialect: databricks
CREATE OR REPLACE TABLE default.default.cars (
  producer STRING not null COMMENT "Producer of the car."
) COMMENT "The Cars table contains cars.";
CREATE OR REPLACE TABLE default.default.producers (
  producer STRING not null primary key COMMENT "A Car Producer."
) COMMENT "The Procuders table contains car producer.";

Desired Output:

-- Data Contract: my-data-contract-id
-- SQL Dialect: databricks
CREATE OR REPLACE TABLE default.default.cars (
  producer STRING not null COMMENT "Producer of the car."
  FOREIGN KEY (producer) REFERENCES default.default.producers(producer)
) COMMENT "The Cars table contains cars.";
CREATE OR REPLACE TABLE default.default.producers (
  producer STRING not null primary key COMMENT "A Car Producer."
) COMMENT "The Procuders table contains car producer.";
@jochenchrist
Copy link
Contributor

jochenchrist commented Oct 29, 2024

The idiomatic way would be to use references:

dataContractSpecification: 0.9.3
id: my-data-contract-id
info:
  title: My Data Contract
  version: 0.0.1
servers:
  production:
    type: databricks
    catalog: default
    schema: default
models:
  cars:
    description: The Cars table contains cars.
    type: table
    fields:
      producer:
        description: Producer of the car.
        type: string
        required: true
        primary: false
        references: producers.producer
  producers:
    description: The Procuders table contains car producer.
    type: table
    fields:
      producer:
        description: A Car Producer.
        type: string
        required: true
        primary: true

Using this references field in sql export is not yet implemented, but should be easy to do. Would you be interested in contributing a PR?

@julianbergerkoerber
Copy link
Author

Alright, let me have a look :)

@julianbergerkoerber
Copy link
Author

After setting up the work environment, I have multiple failing tests without changing anything. Is this behaviour common and are there any tipps to fix this?

@stefannegele
Copy link
Contributor

Hi @julianbergerkoerber . This is not common. Please elaborate your specific problem and errors in a dedicated ticket.

@jochenchrist
Copy link
Contributor

Closed as stale.
If your issues persist, please open a new ticket with a minimal example

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

No branches or pull requests

3 participants