# How to add relationships between objects?

**URL:** <https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406>\
**Category:** ParseSwift SDK\
**Created:** [February 4, 2022, 8:05am UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406 "2022-02-04T08:05:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 8:05am UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/1 "2022-02-04T08:05:25Z")

</div>

Hello,

I have two base classes

```auto
struct ParseCategory: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?
    
    var name: String?
    var index: Int?
    var icon: ParseFile?
    
    var articles: ParseRelation<Self>?
    
    func merge(with object: Self) throws -> Self {
        var updated = try mergeParse(with: object)
        if updated.shouldRestoreKey(\.name, original: object) {
            updated.name = object.name
        }
        if updated.shouldRestoreKey(\.index, original: object) {
            updated.index = object.index
        }
        if updated.shouldRestoreKey(\.icon, original: object) {
            updated.icon = object.icon
        }
        return updated
    }
}

```

```auto
struct ParseArticle: ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var originalData: Data?

    // Custom properties
    var headline: String?
    var author: String?
    var lead: String?
    var body: String?
    var tail: String?
    
    var heroImage: ParseFile?
    var bodyImage: ParseFile?

    var sourceImage: ParseFile?
    var sourceUrl: String?
    
    var category: String?
    var breakingNews: Bool?
    var newsOfTheDay: Bool?

    // Implement your own version of merge
    func merge(with object: Self) throws -> Self {
        var updated = try mergeParse(with: object)
        if updated.shouldRestoreKey(\.headline, original: object) {
            updated.headline = object.headline
        }
        if updated.shouldRestoreKey(\.author, original: object) {
            updated.author = object.author
        }
        if updated.shouldRestoreKey(\.lead, original: object) {
            updated.lead = object.lead
        }
        if updated.shouldRestoreKey(\.body, original: object) {
            updated.body = object.body
        }
        if updated.shouldRestoreKey(\.tail, original: object) {
            updated.tail = object.tail
        }
        if updated.shouldRestoreKey(\.sourceUrl, original: object) {
            updated.sourceUrl = object.sourceUrl
        }
        if updated.shouldRestoreKey(\.category, original: object) {
            updated.category = object.category
        }
        if updated.shouldRestoreKey(\.breakingNews, original: object) {
            updated.breakingNews = object.breakingNews
        }
        if updated.shouldRestoreKey(\.newsOfTheDay, original: object) {
            updated.newsOfTheDay = object.newsOfTheDay
        }
        return updated
    }
}

```

I have newsgroups on my server. Logically, each group should contain a set of news related to this group.

When creating a news, I get the desired group by ID, create news and try to set a one-to-many relationship in the group.

First method:

```auto
func save(for article: Article, with categoryId: String) {
        if let parseCategory = try? ParseCategory.query("objectId" == categoryId).first() {
            let parseArticle = ParseArticle(article: article)
            if let savedArticle = try? parseArticle.save() {
                if let relationOperation = try? parseCategory.operation.addRelation("articles", objects: [savedArticle]) {
                    _ = try? relationOperation.save()
                }
            }
        }
}

```

Other method:

```auto
func save(for article: Article, with categoryId: String) {
        if let parseCategory = try? ParseCategory.query("objectId" == categoryId).first() {
            let parseArticle = ParseArticle(article: article)
            if let savedArticle = try? parseArticle.save() {
                do {
                    guard let newRelations = try parseCategory.relation?.add("articles", objects: [savedArticle]) else {
                        print("Error: should have unwrapped relation")
                        return
                    }
                    newRelations.save { result in
                        switch result {
                        case .success(let saved):
                            print("The relation saved successfully: \(saved)")
                        case .failure(let error):
                            print("Error saving role: \(error)")
                        }
                    }
                }
                catch {
                    print(error)
                }
            }
        }
}

```

None of the methods wants to save the relationship. I studied the documentation and tried different ways, but the relations on the server are empty and do not refer to the added instances.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 8:07am UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/2 "2022-02-04T08:07:05Z")

</div>

![1](https://parse-community-forum-uploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/7/71f640952ce43e1b470b6133379cabd2679f2794.png)

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 8:07am UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/3 "2022-02-04T08:07:44Z")

</div>

![2](https://parse-community-forum-uploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/b/be2da2a24b2bbddb6cec75d29901961f156fced5.png)

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 4, 2022, 12:54pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/4 "2022-02-04T12:54:33Z")

</div>

What does your Xcode console log say? Is it printing your success statement or error messages? If it’s an error, what’s the error?

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 1:48pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/5 "2022-02-04T13:48:34Z")

</div>

It says: `The relation saved successfully`

Maybe I added the relation incorrectly? That’s why I gave examples of my code. Can the category itself be updated? I tried to call the save method on the category, but the result did not change

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 4, 2022, 1:52pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/6 "2022-02-04T13:52:45Z")

</div>

If it says it saved, you should run the query code in the Playgrounds… I also don’t recommend calling the synchronous methods as you are doing, you should be using async/await or completion.

You are probably running into an issue with Parse Dashboard. Hit your browsers “refresh” button. Just clicking on the links won’t refresh the dashboard. I mention this problem with `_Role`, but haven’t had a chance to open an issue on Dashboard:

> <https://github.com/parse-community/parse-dashboard/pull/2010#issuecomment-1025153660>
>
> @visualfanatic I appreciate all of your updates, the dashboard is looking nice! …Not sure if it’s related to this, but I when \_Role is updated I have to refresh the browser to see updated \`roles\` in 4.0.0.alpha.15. If I click other classes in the dashboard and then back to \_Role, the updates wont show until I do a manual refresh. I’ll open a formal issue shortly…

When in doubt, the Playground code is runnable, you should fork the repo, change the settings to point to your server and run the Playgrounds directly:

> <https://github.com/parse-community/Parse-Swift/blob/5307210d97dedeb88297c653b0675ff1ef920221/ParseSwift.playground/Sources/Common.swift#L4-L19>

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 2:05pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/7 "2022-02-04T14:05:07Z")

</div>

I printed my console:

```auto
The relation saved successfully: ParseCategory ({"objectId":"Eo3ISyIosZ","updatedAt":{"__type":"Date","iso":"2022-02-04T13:58:47.911Z"}})
Printing description of result.success:
▿ ParseCategory ({"objectId":"Eo3ISyIosZ","updatedAt":{"__type":"Date","iso":"2022-02-04T13:58:47.911Z"}})
  ▿ objectId : Optional<String>
    - some : "Eo3ISyIosZ"
  - createdAt : nil
  ▿ updatedAt : Optional<Date>
    ▿ some : 2022-02-04 13:58:47 +0000
      - timeIntervalSinceReferenceDate : 665675927.911
  - ACL : nil
  - originalData : nil
  - name : nil
  - index : nil
  - icon : nil
  - articles : nil

```

A category with this ID exists on the server. But when I print to the console, I see nil values for this category. Maybe I missed any update?

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 4, 2022, 2:58pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/8 "2022-02-04T14:58:09Z")

</div>

What’s the output when you query the relations:

> <https://github.com/parse-community/Parse-Swift/blob/5307210d97dedeb88297c653b0675ff1ef920221/ParseSwift.playground/Pages/12%20-%20Roles%20and%20Relations.xcplaygroundpage/Contents.swift#L319-L373>

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 3:19pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/9 "2022-02-04T15:19:06Z")

</div>

You are absolutely right! This is a panel issue. I installed several versions, including beta, but the problem remained. I reset the cache on another browser, the entries are displayed once, after they disappear. But in the code, when requested, everything comes up correctly. This is strange, since I have several applications in this panel, in another application all relationships are displayed correctly.

A lot of time wasted. Thank you all for your help!

 ![Screenshot 2022-02-04 at 18.03.26](https://parse-community-forum-uploads.s3.dualstack.us-east-1.amazonaws.com/original/2X/6/6e63091e7905142712310c4bf88fc7da4ced7350.png)

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 4, 2022, 3:25pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/10 "2022-02-04T15:25:29Z")

</div>

Should populate whenever you hit “refresh”. I recommend [opening an issue on Parse Dashboard](https://github.com/parse-community/parse-dashboard/issues). You can reference this thread and with the GitHub comment I posted earlier. When describing the issue, it may help to do a screen recording of before refresh and after.

---

<div class="post-metadata">

**Author:** ![InstaRobot](https://community.parseplatform.org/user_avatar/community.parseplatform.org/instarobot/32/1045_2.png) [@InstaRobot](https://community.parseplatform.org/u/InstaRobot)\
**Post date:** [February 4, 2022, 3:29pm UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/11 "2022-02-04T15:29:12Z")

</div>

Certainly. I will do so. Today I will prepare all the data on this problem and send a description of the problem

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [September 27, 2022, 11:52am UTC](https://community.parseplatform.org/t/how-to-add-relationships-between-objects/2406/12 "2022-09-27T11:52:35Z")

</div>

This should be fixed via [#2275](https://github.com/parse-community/parse-dashboard/pull/2275)
