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

many to many relationship #454

Closed
FCO opened this issue Nov 18, 2020 · 2 comments
Closed

many to many relationship #454

FCO opened this issue Nov 18, 2020 · 2 comments

Comments

@FCO
Copy link
Owner

FCO commented Nov 18, 2020

One of the best ways of doing a many-to-many relationship is something like this:

model Bli {...}

model Bla {
   has $!id   is serial;
   has $.a    is column;
   has @.bles is relationship( *.bla-id, :model<Ble>);
   method blis { self.bles.join-model(Bli, *.bli-id == *.id) }
}

model Ble {
   has $.bla-id is column{ :id, :references{ .id }, :model-name<Bla> };
   has $.bli-id is column{ :id, :references{ .id }, :model-name<Bli> };
}

model Bli {
   has $!id is serial;
   has $.i  is column;
   has @.bles is relationship( *.ble-id, :model<Ble>);
   method blas { self.bles.join-model(Bla, *.bla-id == *.id) }
}

red-defaults "SQLite";

schema(Bla, Ble, Bli).create;

Bla.^create: :a<bla1>;
Bli.^create: :i<bli1>;

Ble.^create: :1bla-id, :1bli-id;

.say for Bla.^load(1).blis 

But that has some problems, for example, it's not so easy to create new objects, so I think it should be improved.
Maybe something like this:

model Bla {
   has $!id   is serial;
   has $.a    is column;
   has @.bles is relationship( *.bla-id, :model<Ble>);
   has @.blis is relationship( *.bli-id, :many-to-many{ .bles }, :model<Bli> );
}

model Ble {
   has $.bla-id is column{ :id, :references{ .id }, :model-name<Bla> };
   has $.bli-id is column{ :id, :references{ .id }, :model-name<Bli> };
   has $.bla is relationship(*.bla-id, :model<Bla>);
   has $.bli is relationship(*.bli-id, :model<Bli>);
}

model Bli {
   has $!id is serial;
   has $.i  is column;
   has @.bles is relationship( *.ble-id, :model<Ble>);
   has @.blas is relationship( *.bla-id, :many-to-many{ .bles }, :model<Bla> );
}

But I'm not sure about that syntax. The important part is the @ sigil and some way of specifying 2 model/key pairs.

Something that should be defined is that we should use an intermediate to-many relationship:

has @.blis is relationship( *.bli-id, :many-to-many{ .bles }, :model<Bli> );

Or if we should specify 2 relationships:

has @.blis is relationship( :many{ .bles }, :to-many{ .bli } );

Or 2 pairs of column/model:

has @.blis is relationship( *.bli-id, :model<Bli>, :intermediate{ .bla-id }, :intermediate-model<Ble> );

Any suggestions?

@FCO
Copy link
Owner Author

FCO commented Nov 18, 2020

maybe all should be accepted (with a more consistent syntax)

@FCO
Copy link
Owner Author

FCO commented Feb 22, 2021

It's easily done with $post.post-tags>>.tag, so no need for special syntax.

@FCO FCO closed this as completed Feb 22, 2021
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

1 participant