Support Topics Documentation Slack YouTube Blog

Cannot create relation between tables. "Cannot find object with id - null"

Hi all. I am having trouble creating relations between tables. I keep getting “Server returned an error Unable to create relation. Cannot find object with id - null.” as the exception, leading me to believe that the child object in the relation does not have an “objectId” but that assumption has proven false. I am not sure what I am doing wrong with this. I have also noticed that the “created” and “updated” fields of all my data models is {1/1/0001 12:00:00 AM}, which is weird.

I have a parent object that looks like this…

 public class TeamWebDataModel
{
    public TeamWebDataModel()
    {
    }

    public string Name { get; set; }
    public CoachWebDataModel Coach { get; set; }
    public CoachWebDataModel AssistantCoach { get; set; }

    public string objectId;
    public DateTime created;
    public DateTime updated;
}

and a child object that looks like…

 public class CoachWebDataModel
{
    public CoachWebDataModel()
    {
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string SignInCode { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public string DisplayName
    {
        get { return $"{FirstName} {LastName}"; }
    }

    public string objectId;
    public DateTime created;
    public DateTime updated;
}

The block of code I am using to try and create a new Team(parent) and then set existing 1:1 children (Coach and AssistantCoach) to it is…

                    var team = new TeamWebDataModel
                {
                    Name = Name,
                    Coach = _selectedCoach,
                    AssistantCoach = _selectedAssistantCoach
                };

                await Backendless.Data.Of<TeamWebDataModel>().SaveAsync(team);

                Backendless.Data.Of<TeamWebDataModel>().SetRelation(
                 team,
                 $"{nameof(TeamWebDataModel.Coach)}:{nameof(CoachWebDataModel)}:1",
                 new object[] { _selectedCoach });

                Backendless.Data.Of<TeamWebDataModel>().SetRelation(
                    team,
                    $"{nameof(TeamWebDataModel.AssistantCoach)}:{nameof(CoachWebDataModel)}:1",
                    new object[] { _selectedAssistantCoach });

Just to be safe I also tried retrieving the child object directly from the API before using it instead of relying on the cached object…

var selectedCoach = await Backendless.Data.Of<CoachWebDataModel>().FindByIdAsync(_selectedCoach.objectId);

Well, I got this one figured out. It is not the child object that is missing the id, but the parent. I fixed it by…

  var savedTeam = await Backendless.Data.Of<TeamWebDataModel>().SaveAsync(team);