Parse & JS: Relational data

This entry is part 22 of 25 in the series Parse & Javascript

As with other relational database, objects may have relationships with other objects.

For example, in a blogging application, a Post object may have many Comment objects.

Let’s look at how we can create a one-to-one relationship in Parse.

<!doctype html>
    <script type='text/javascript' src=''></script>
    <script type='text/javascript' src=''></script>


    Parse.initialize('kYqz3wRHRa0GDX7viRIVXKDVNIaI0q7Fw6Ms3pv2', 'yPjRYQmwZC2MthUPrer1mU9dDpr84xVQjmEtT7fS');

    // Declare the types.
    var Post = Parse.Object.extend('Post');
    var Comment = Parse.Object.extend('Comment');

    // Create the post
    var myPost = new Post();
    myPost.set('title','I am Hungry');
    myPost.set('content', 'Where should we go for lunch?');, {
        success: function(myComment) {
            // The object was saved successfully.
            document.write('Post Saved Ok');
            var id =;
            // Create the comment            
            var myComment = new Comment();
            myComment.set('content', 'Let us do Sushirrito.');
            myComment.set('parentId', id);
            myComment.set('parent', myPost);

  , {
                success: function(myComment) {
                    // The object was saved successfully.
                    document.write('Comment Saved OK');
                error: function(myComment, error) {
                    // The save failed.
                    // error is a Parse.Error with an error code and description.
                    document.write('Comment Saved Not Ok');

        error: function(myComment, error) {
            // The save failed.
            document.write('Post Saved Not Ok');

Try The Code

After the first run, you can see Post and Comment objects created in the App.


If you click on Comment object, you will see a parent column with objectid of the parent, which is the Post object.

This is a actually a pointer to the parent.  If you click on it, you will be brought to the parent object.

I have also kept the objectId of the parent in the parentId column of Comment object.


Series Navigation<< Parse & JS: Counting objects
Parse & JS: Retrieving comment using the parentId >>

Leave a comment

Leave a Reply