hasura / hasura/graphql-engine

Optionality of a compound-key relationship wrong

Open
#8,683 0 comments 0 reactions 0 assignees View on GitHub
k/bug
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: 2.8.4
CLI Version (for CLI related issue):

### Environment

OSS Docker version following https://hasura.io/docs/latest/graphql/core/getting-started/docker-simple/

### What is the current behaviour?

For the following database

```sql
CREATE TABLE project (
id SERIAL NOT NULL,
name VARCHAR(300) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE subcontract (
id SERIAL NOT NULL,
name VARCHAR(300) NOT NULL,
project_id INTEGER NOT NULL,
PRIMARY KEY (project_id, id),
FOREIGN KEY (project_id) REFERENCES project (id)
);

CREATE TABLE invoice (
id SERIAL NOT NULL,
title VARCHAR(300) NOT NULL,
subcontract_id INTEGER NOT NULL,
project_id INTEGER NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (project_id, subcontract_id) REFERENCES subcontract (project_id, id),
FOREIGN KEY (project_id) REFERENCES project (id)
);
```

The following definition is generated for the invoice table

```graphql
"""
columns and relationships of "invoice"
"""
type invoice {
id: Int!

"""An object relationship"""
project: project!
project_id: Int!

"""An object relationship"""
subcontract: subcontract # this field is not marked as required
subcontract_id: Int!
title: String!
}
```

Where the subcontract relation is marked as optional where it should be required

### What is the expected behaviour?

The following definition is generated for the invoice table

```graphql
"""
columns and relationships of "invoice"
"""
type invoice {
id: Int!

"""An object relationship"""
project: project!
project_id: Int!

"""An object relationship"""
subcontract: subcontract! # this field should be marked as required
subcontract_id: Int!
title: String!
}
```

Where the subcontract relation is marked should be marked as required because all keys part of the foreign key are required

### How to reproduce the issue?

1. Create the tables in with the provided sql
2. Track all tables
3. Track all relationships

### Screenshots or Screencast

Not apply

### Please provide any traces or logs that could help here.

No error thrown

### Any possible solutions?

**Expected behaviour**: If all fields of a foreign key are required the field should be marked as required, otherwise it must be optional.
**Current behaviour**: always it is optional no matter what.

### Can you identify the location in the source code where the problem exists?

No idea

### If the bug is confirmed, would you be willing to submit a PR?

No idea where to start

### Keywords

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.