SwiftData: Unknown Relationship Type - nil

I'm getting an error: Unknown Relationship Type - nil when using SwiftData and CloudKit. I've searched for this on Google, but seems like one has experienced it before. This is on iOS 18.

I have a Transaction model:

@Model
final class Transaction {
    
    var timestamp: Date = Date()
    var note: String    = ""
    var cost: Cost?     = nil
    var receipt: Receipt? = nil
    
    //Relationships
    var merchant: Merchant? = nil
    var category: TransactionCategory? = nil
    var tags: [Tag]?        = nil
    
    init(timestamp: Date) {
        self.timestamp = timestamp
    }
}

This has a relationship with TransactionCategory.

@Model
final class TransactionCategory {
    
    var id = UUID()
    var name: String = ""
    
    //Relationship
    var transactions: [Transaction]? = []
    
    init() { }
    
    init(name: String) {
        self.name = name
    }
}

I've tried using @Relationship here in TransactionCategory, but it didn't make a difference.

There is Picker that allows you to select a Category. In this case I created a random one, which was inserted into the context and saved. This is successful by the way. When you press a done button, a Transaction is created and the Category modified like this: newTransaction.category = category. It fails as this point with that error.

I also tried to create the Transaction, insert into the context, and then update the Category but it then failed at the context insertion, prior to updating the Category.

As you can see, I have another model called Merchant. When you press the done button, if you've typed in a Merchant name, it will create it, insert it into the context and then update the transaction as such:

newTransaction.merchant = getMerchant()

private func getMerchant() -> Merchant? {
        //Create merchant if applicable
        if merchant.name.isEmpty == false {
            if let first = merchants.first(where: {$0.name == merchant.name.trim()}) {
                // Set to one that already exist
                return first
            } else {
                // Insert into context and insert into transction
                context.insert(merchant)
                return merchant
            }
        }
        
        return nil
    }

This code works fine and Merchant has the same relationship with Transaction as Category does.

Does anyone have any idea what could be causing this problem?

Answered by Xavier-k in 797730022

Well I figured out the issue and it wasn't related to TransactionCategory at all. When using CloudKit you have to use optional relationships with default values. Apparently doing this: var tags: [Tag]? = nil is not acceptable. You have to use an empty string as the default value.

Accepted Answer

Well I figured out the issue and it wasn't related to TransactionCategory at all. When using CloudKit you have to use optional relationships with default values. Apparently doing this: var tags: [Tag]? = nil is not acceptable. You have to use an empty string as the default value.

SwiftData: Unknown Relationship Type - nil
 
 
Q