# Pastebin uv1dvAkV use Red; model Post { ... } model Photo { ... } model Comment { has UInt $.id is serial; has Str $.body is column; has UInt $.commentable-id is column; has Str $.commentable-type is column; has Post $.post is relationship{ $^a.commentable-id == $^b.id && $^a.commentable-type eq "post" } has Photo $.photo is relationship{ $^a.commentable-id == $^b.id && $^a.commentable-type eq "photo" } } model Post { has UInt $.id is serial; has Str $.title is unique; has Comment @.comments is relationship{ $^b.commentable-id == $^a.id && $^b.commentable-type eq "post" } } model Photo { has UInt $.id is serial; has Str $.path is unique; has Comment @.comments is relationship{ $^b.commentable-id == $^a.id && $^b.commentable-type eq "photo" } } red-defaults "SQLite"; schema(Comment, Post, Photo).create; my $post = Post.^create: :title, :comments[{ :body }]; my $photo = Photo.^create: :path; $photo.comments.create: :body; .say for Comment.^all.grep: *.post.title eq "bla"; .say for Comment.^all.grep: *.photo.path eq "ble"; say "comment: { .id }; post: ", .post for Comment.^all; say "comment: { .id }; photo: ", .photo for Comment.^all;