swiftparse-platformpfobject

Parse.com error: can't add a non-pointer to a relation


I have Messages class table and inside the table contains "messageTextColumn" column and "user" column.

This "user" column, I created it with type pointer and the target class is _User which is parse main class.

I want to store both user's saved messages and the userId so that I can query it and show it in that user's message tableview cell which will show the list of every messages that the user had saved in my app.

This is my code:

 @IBAction func addMessage(sender: AnyObject) {
    var newMessage = addMessageText.text

    let message = PFObject(className: "Messages")
    var user = PFUser.currentUser()
    message["messageTextColumn"] = newMessage
    message["user"] = user

    message.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
        if (success) {
            println("added to Message Class")
           println(user)
            message.saveInBackground()
        } else {
            // Error saving message
        }
    }
}

The problem is when I run this, the log shows:

[Error]: can't add a non-pointer to a relation (Code: 111, Version: 1.7.4)

and the message was not saved on Parse at all but when I deleted this line of code:

message["user"] = user

I was able to save the message to Parse Messages class table but not the userID of the user who saved it.


Solution

  • In your code you're trying to add an entire user object when you say you only want to add the user's object ID. If you only want to add the user's object ID then you would do the following:

    // ...
    message["user"] = PFUser.currentUser().objectId
    

    And in your database your user column would be of String type.

    If you actually wanted to add the entire user object as a relation to your message object then you would add the user like you did:

    // ...
    message["user"] = PFUser.currentUser()
    

    And in your database your user column would be of Pointer type to _User.