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

feat: constraints through smart comments (view foreign keys, etc) #365

Merged
merged 19 commits into from
Dec 19, 2018

Conversation

benjie
Copy link
Member

@benjie benjie commented Dec 7, 2018

  • relations @foreignKey (col1, col2) references other_table (col3, col4) (fix(jwt): handle bigint / numeric in JWTs #376)
  • primary key for nodeID / etc @primaryKey (col1, col2)
  • ability to override non-null-ness of columns
  • soft links (e.g. allowing a value to reference another table where a row may or may not exist)

Fixes graphile/crystal#376

@itaied246
Copy link

Getting this error for view foreign key:

create table post (
  id text primary key
);
comment on table post is E'@name post_table
@omit';

create table offer (
  id serial primary key,
  post_id text references post(id) not null,
);
comment on table offer is E'@name offer_table
@omit';

create view post_view as
  SELECT 
    post.id,
    FROM mydb.post post;
comment on view post_view is E'@name posts
@uniqueKey id';

create view offer_view as
  SELECT
    offer.id,
    offer.post_id

    FROM mydb.offer offer;
comment on view offer_view is E'@name offers
@uniqueKey id
@foreignKey (post_id) references posts';
A fatal error occurred when building the initial schema, so the process will now exit. Error details:

TypeError: Cannot read property 'namespaceId' of undefined

@benjie
Copy link
Member Author

benjie commented Dec 10, 2018

That’s a misleading error 👍

@itaied246
Copy link

Continue the previous example:

comment on view offer_view is E'@name offers
@uniqueKey id
@foreignKey (post_id) references post_view (id)';

Throws:

A fatal error occurred when building the initial schema, so the process will now exit. Error details:

Error: No columns specified for 'mydb.post_view' (oid: 119745) and no PK found (@foreignKey (post_id) references post_view(id)).

@benjie
Copy link
Member Author

benjie commented Dec 11, 2018

@itaied246 Thanks for these; I've added tests for them and fixed the missing parenthesis which caused the "No columns specified" error. Note you've used @uniqueKey but for it to work like in PostgreSQL where you can omit columns you must instead use @primaryKey (like you would in PostgreSQL).

@benjie
Copy link
Member Author

benjie commented Dec 11, 2018

This now opens up "soft references" - i.e. if you have to tables:

Clowns:

id name
1 Marbles
2 Boris
3 Smiley

People:

id name
7 Alice
8 Boris
9 Caroline

Then you could link them together to find people named after clowns, but the constraint is not enforced by the database:

comment on table people is E'@foreignKey (name) references clowns (name)|@fieldName namedAfterClown|@foreignFieldName peopleSharingName';
{
  allPeople {
    nodes {
      id
      name
      namedAfterClown { id name }
    }
  }
}
{
  allPeople: {
    nodes: [
      { id: 7, name: "Alice", namedAfterClown: null },
      { id: 8, name: "Boris", namedAfterClown: { id: 2, name: "Boris" } },
      { id: 9, name: "Caroline", namedAfterClown: null },
    ]
  }
}

(You may have also noticed that I've allowed you to tag these fake constraints too, using the pipe syntax instead of newlines. This is going to get old fast.)

@benjie benjie merged commit 7c712a9 into master Dec 19, 2018
@benjie benjie deleted the smart-comment-relations branch December 19, 2018 16:26
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.

None yet

2 participants