TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData: improper model with nonsensical relationship definitions

Forums > Swift

I'm not sure how to model basic Twitter with SwiftData, at the beginning I even went to an SQL-like approach with no relationships and a bunch of pivots (which will work, but there is no point in using SwiftData in this case), but decided to try @Relationships and now I'm getting an error from the topic's title. It's more like a beg for help rather than a question, I'd like to understand how to properly model smth like this using SwiftData 🫠:

import SwiftData
import LocalExtensions

public final class TweetModel: Equatable, Identifiable, Sendable {
    public var id: String
    public var createdAt: Date

    @Relationship(inverse: \UserModel.likedTweets)
    public var author: UserModel

    public var repostSource: TweetModel?

    public var replySource: TweetModel?

    @Relationship(inverse: \TweetModel.replySource)
    public var replies: [TweetModel]

    @Relationship(inverse: \TweetModel.repostSource)
    public var reposts: [TweetModel]

    @Relationship(inverse: \UserModel.likedTweets)
    public var likes: [UserModel]

    public var content: String

    public init(
        id: USID,
        createdAt: Date = .now,
        author: UserModel,
        repostSource: TweetModel? = nil,
        replySource: TweetModel? = nil,
        replies: [TweetModel] = [],
        reposts: [TweetModel] = [],
        likes: [UserModel] = [],
        content: String
    ) { = id.rawValue
        self.createdAt = createdAt = author
        self.repostSource = repostSource
        self.replySource = replySource
        self.replies = replies
        self.reposts = reposts
        self.likes = likes
        self.content = content
import SwiftData
import LocalExtensions

public final class UserModel: Equatable, Identifiable, Sendable {
    public var id: String

    public var username: String

    public var password: Data
    public var displayName: String
    public var bio: String
    public var avatarURL: URL?

    @Relationship(deleteRule: .cascade)
    public var tweets: [TweetModel]

    public var likedTweets: [TweetModel]

    @Relationship(inverse: \UserModel.followers)
    public var follows: [UserModel]

    public var followers: [UserModel]

    public init(
        id: USID,
        username: String,
        password: Data,
        displayName: String = "",
        bio: String = "",
        avatarURL: URL? = nil,
        tweets: [TweetModel] = [],
        likedTweets: [TweetModel] = [],
        follows: [UserModel] = [],
        followers: [UserModel] = []
    ) { = id.rawValue
        self.username = username
        self.password = password
        self.displayName = displayName = bio
        self.avatarURL = avatarURL
        self.tweets = tweets
        self.likedTweets = likedTweets
        self.follows = follows
        self.followers = followers

Here is a project you can clone and run to get improper model with nonsensical relationship definitions crash 🌚


Well, after writing this post I noticed that I was trying to connect with UserModel.likedTweets instead of UserModel.tweets


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.