Create a single relationship reference between collection entries in Keystatic

Share this video with your friends

Social Share Links

Send Tweet
Published 6 months ago
Updated a month ago

In this lesson, you'll create a relationship reference between posts and authors. You'll relate each post to an existing author, and see how you can edit these relationships from the Admin UI or from the code directly.

Instructor: [0:00] We have Posts and Authors collections, and now we want to create a relationship between the two. For each post, we want to be able to relate it to an existing author. This is our keystatic.config file. In the posts collection, in the schema, where we have a title and a content fields, let's add an author field.

[0:19] This is going to be fields., and you can see the list of possible fields in Keystatic. We're going to use relationship. Let's give that a label of 'Author'. The relationship field also needs to know what collection you want to reference something from, so collection will be 'authors', which corresponds to the authors collection right here. That's all we need to get set up.

[0:44] Now, if I go edit one of the existing posts, we now see an Author picker. I can select one of the existing authors or start typing the name to find it, and I can relate this post to simon. Back in the code, if I go to check the first-post here, you can see that now it's been related to simon.

[1:04] Mind you, rather than a solid and persisting relationship, this is more a slug reference. The value will be saved as a string, and if the author slug changes, this will not get updated, just something to keep in mind. All right?

[1:17] Let's relate the other post to the other author. Since we've always been updating things from the Admin UI, let's do it from code this time. Here, I'll go Author, jed, which is the slug of the other author, and I'll save that. Let's go verify in the Admin UI that the second post is connected to jed. Yep, it is.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today