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.