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

`#[belongs_to]` doesn't support composite foreign key #1413

Closed
KeenS opened this Issue Dec 19, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@KeenS
Contributor

KeenS commented Dec 19, 2017

The title is describing problem.

Suppose you have this schema

CREATE TABLE users (
  id SERIAL NOT NULL PRIMARY KEY
  -- ...
);
CREATE TABLE groups (
  id SERIAL NOT NULL PRIMARY KEY
  -- ...
);
CREATE TABLE group_members (
  user_id INTEGER NOT NULL REFERENCES users(id),
  group_id INTEGER NOT NULL REFERENCES groups(id),
 -- ...
  PRIMARY KEY (user_id, group_id)
);

CREATE TABLE member_comments (
  id SERIAL NOT NULL PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id),
  group_id INTEGER NOT NULL REFERENCES groups(id)
 -- ...
);

and you'd like to derive Association to member_description

#[derive(Queriable, Ideintfiable)]
#[foreign_key(group_id, user_id)]
struct GroupMember {
  group_id: i32,
  user_id, i32,
}

#[derive(Queriable, Identifiable, Associations)]
#[belongs_to(GroupMember, foreign_key = ???)]
struct MemberComment {
  id: i32,
  group_id: i32,
  user_id: i32,
}

current implementation supports only one foreign key. Are there any chance to allow composite foreign key in belongs_to attr?

Checklist

  • I have already looked over the issue tracker for similar issues.
@sgrif

This comment has been minimized.

Member

sgrif commented Dec 19, 2017

At the moment we would not be able to support composite foreign keys here without making a breaking change to BelongsTo. At the moment we're not considering anything that would require a major version bump.

@sgrif sgrif closed this Dec 19, 2017

@KeenS

This comment has been minimized.

Contributor

KeenS commented Dec 20, 2017

Thank you. I'll use surrogate keys.

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