Backendless Support
 
Answered

Table relationship with user

I created a new table using the web admin, and I'd like to create a many-to-one relationship with the user table. When I click the "Relationships" button in the data view for the new table, the User table isn't listed in the drop down to creation a relationship with. Not sure how I got about this.

Leave a Comment

Comments (13)

photo
2

Hi Devin, rather than creating a relationship, you could add a column into your table which would contain user ID (either internally assigned or just the identity property value). This would let you easily select the objects by the ID of the user. Would that work for you?

photo
1

I'd prefer to create a relationship between the users table and my other tables.

First, because my "users" table doesn't show any uniquely identifiable key or user Id. Something is way wrong with the users table.

Second, I'd like foreign key constraints generated automatically instead of programmatically.

photo
1

The same issue.

I need to create object "group".

So I need "teams" table with users id like column.

photo
1

every BackendlessUser object contains a unique ID assigned by Backendless. For instance, with Android, the ID can be obtained by using the getUserId() method (see BackendlessUser Class Definition at: http://backendless.com/documentation/users/android/index.html?android_core_classes.htm). You can use that ID to reference a particular user from any other table.

photo
1

Is this now supported? In the User console, I see the "Add Relationship" button, which I've done, but I don't understand how insert the relationship data.

photo
1

I have the same question as Devin.

In my Todos table, I add a one-to-one relationship for the field "owner" with the Users table.

I then try to create a new Todo:

    Backendless.Persistence.save( "Todo", {description: "test", owner: user} );

and I get back

    POST https://api.backendless.com/v1/data/Todo 403 (Forbidden)

    message: "Missing ___class property for entity: owner"

I try again with

    user.___class = "User"

    Backendless.Persistence.save( "Todo", {description: "test", owner: user} );

    >POST https://api.backendless.com/v1/data/Todo 403 (Forbidden)

    >message: "Relation type update is prohibited - property owner must relate to table Users but not User"

When I try to add a different relationship -- adding a relationship from Todo to "Foo" -- I am able to find an existing "Foo" entry in the DB, add "foo.___class = "Foo" (I don't know why this step is necessary, since foo was handed back to me from the DB....), and then save:

    foo = fooDB.findById("408E6690-7AB0-C162-FF2C-F25DE72B1A00" );

    foo.___class = "Foo";

    Backendless.Persistence.save( "Todo", {description: "test", foo: foo} );

this works. So why not with users?

photo
1

Hi Sam,

When you need to create an association with a user, you should establish the relationship going from User to the object. So the code you showed would need to be changed to something like this:

user[ "todo" ] = [ {description:"test1" }, {description:"test2" } ]

Backendless.Users.save( user );

Notice the "todo" property in the user object contains an array of todo objects, that way you have a one-to-many relationship between user and his todos. However, you could have a one-to-one relationship as well:

user[ "todo" ] = {description:"test1" };

Backendless.Users.save( user );

Hope this helps.

Mark

photo
1

Hi Mark,

Thanks for that answer. However, what about a one-to-many relationship between one Todo and many owners (or "watchers" or whatever) on that item? That doesn't seem to work either.

Or how about between two users? I can't get that to work. (Speaking of which, `Backendless.Users.save( user );` doesn't seem to work in my recently-downloaded version of the SDK -- I think it should be Backendless.UserService.update( user ))

It seems strange that the Users table is treated so differently from the other tables -- a relationship from one table to a users table seems like quite a common need.

photo
1

Hi Sam,

the one-to-many relationship between one Todo and multiple watchers/owners would still be possible with the approach I suggested. You simply would update each user/owner object with a reference to a Todo object. If the Todo object is already in the storage, then Backendless would create a link between the User and the Todo, that is if a Todo already has objectId assigned, it will not be created again.

As for linking multiple users together, we will look into it.

The Users table is special in many ways. Mostly because of all the extra business logic around user registration, login, social integration, email confirmation, etc.

You are correct about the SDK call, it is indeed Backendless.UserService.update. I think Backendless.Users.save looks a bit better.

Cheers,

Mark

photo
1

Hi All,

I have a question regarding Primary Key & Foreign Key in backendless.

How can I give Primary Key to a column and use that col as FK in other table. It is quite much required now at this stage of my project.

Hope for a quick and kind reply from you friends. Thanks in advance.

Jay

photo
1

Hi, Jay,

Backendless operates FKs under the hood, all you need to know is about Relations. Read more at documentation page.

https://backendless.com/documentation/data/android/data_relations.htm

Best, Artur.

photo
1

How can I create a client at the same time create a user linked to this client, my relationship is 1: 1?

photo
1

My name is Steven Linda from Scotland, i want to testify about a great spell caster that help me cast a spell that bring my husband back to me without any delay. I broke up with him with just little misunderstanding hoping we will get back shortly,but things was growing worse until i contacted Dr.Manbela who help me with his historical powers to bring him back, i have never believed in a spell caster until i come across him, it will be of great sin if i should go out from here without dropping the contact of this great spell caster, in case you need the help of this great spell caster you can contact him through his email: once you contact him all your problems will be over ,once again i say very big thanks to you sir for helping me to recover my husband back, and please sir keep your good work because people need your help and in their lives ,once more thank you Dr Manbela

Have a good day.

contact him via; manbelaspelltemple1@gmail.com or through his whatsapp number+2348112060023