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

Feature request: one-to-one relations the other way also (breaking change) #432

Closed
benjie opened this issue Apr 9, 2017 · 11 comments
Closed

Comments

@benjie
Copy link
Member

benjie commented Apr 9, 2017

CREATE TABLE foo (
  id serial not null,
  note text
);

CREATE TABLE bar (
  foo_id integer not null primary key references foo,
  extended_note text
);

It would be nice in this case if foo had a barByFooId record rather than barsByFooId collection, since the bar.foo_id column is unique/primary key.

(It works fine if you model this the other way:

CREATE TABLE foo (
  id serial not null,
  bar_id integer references bar
  note text
);

CREATE TABLE bar (
  id serial not null,
  extended_note text
);

but this isn't always optimal)

@jmealo
Copy link

jmealo commented Jun 28, 2017

Any news on this?

@benjie
Copy link
Member Author

benjie commented Sep 28, 2017

Raising the priority of this because I've hit it again; and v4 would be the perfect time to introduce a breaking change for the better.

@chadfurman
Copy link
Collaborator

barsByFooId will work, though, right? It just returns a collection with only one element?

And is it a breaking change because it introduces a new function name?

@benjie
Copy link
Member Author

benjie commented Oct 4, 2017

It's a breaking change because if we detect the relation to be one-to-one we'd be removing the false one-to-many relationship that's currently present and using the one-to-one instead.

I think we can detect it quite easily: if the reference is on a primary or unique key then it's one-to-one.

We might be able to add some command-line flags to retain the previous behaviour, but that adds more work.

@benjie
Copy link
Member Author

benjie commented Nov 29, 2017

@benjie
Copy link
Member Author

benjie commented Dec 3, 2017

Note to self: add an option people can use to gain backwards compatibility.

@hmaurer
Copy link

hmaurer commented Dec 16, 2017

I just want to emphasise that it would be nice to have it on "UNIQUE" references as well as foreign keys (I need it for one of my projects). Looking forward to see this feature land!

@chadfurman
Copy link
Collaborator

Does this mean that if we have a join table, it will register as a one-to-one relationship because the foreign key is a primary or unique key?

@benjie
Copy link
Member Author

benjie commented Feb 3, 2018

No, this is only for a single column primary key (or other unique key); I think there’s another issue for many-to-many relations.

@benjie
Copy link
Member Author

benjie commented Feb 23, 2018

Added in graphile/graphile-engine#177

@benjie benjie closed this as completed Feb 23, 2018
@benjie benjie added this to the 4.0 milestone Aug 15, 2018
@shellscape
Copy link

shellscape commented Aug 22, 2022

@benjie we've got legacyRelations: 'omit' in our options as a standard but still seeing some FooConnection on one-to-one relationships where a table has a foreign key on another table's primary key. Is there an additional trick to getting v4 to behave in one-to-one?

Edit: OK this was a one-to-one with a view, and required a @primaryKey smart tag on the view. Hopefully that's useful for future people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants