Back

Relation Issue

  • 0
  • Self Hosted
thelongestsigh
9 Dec, 2024, 17:23

Me and my team are having some issues with relations in .NET SDK. We have to first create an element and then update it with the relation of the object before it works, otherwise it gives permission error (yes i've set the right permissions). Any idea when a new SDK or a new version of the selfhosted appwrite docker image will release?

We're depeding on Appwrite for a project and it would be nice if relations worked as expected. I've seen a lot of progression on their Github but no new release? PS yes i know it's in beta but this seems like a basic requirement to us...

TL;DR
Developers are discussing the inefficiencies and issues with using relationships in creating and updating data with the SDK. They discuss the need to migrate away from using relationships until they are revamped. The code examples shared show the usage of the SDK in creating and updating activities and groups. The discussion involves checking permissions, creating items separately, and ensuring relationships are set up correctly. Solution: Separate the creation of related items and then update the parent record to relate to the child for a successful operation. Make sure permissions are set correctly for the collections involved.
thelongestsigh
9 Dec, 2024, 17:24

If needed i can privately share the project files

Kenny
9 Dec, 2024, 19:07

So when you try to create a new record with a relationship to another new record it fails saying you don't have the perms. But when you create each separately then update the parent record to relate to the child it works fine?

thelongestsigh
9 Dec, 2024, 21:12

Yes exactly! Can you help me?

Kenny
9 Dec, 2024, 21:21

Can you successfully create the both items in the SDK?

Kenny
9 Dec, 2024, 21:21

in seperate calls?

Kenny
9 Dec, 2024, 21:21

Can you send screenshot of the permissions you have setup for both collections please?

thelongestsigh
9 Dec, 2024, 21:40

Yes in separate calls it works fine

thelongestsigh
9 Dec, 2024, 21:41

all permissions are set like this

Kenny
9 Dec, 2024, 21:44

Do you have more than one relationship? Can you send the code just for the create document method, leaving out tokens

thelongestsigh
9 Dec, 2024, 21:46

So just for explanation i've set up a abstract base class which is a wrapper around the appwrite SDK, just for easy creating of object inside the project. Which is the following method:

thelongestsigh
9 Dec, 2024, 21:48
TypeScript
public async Task<T> Create(T item)
{
    try
    {
        var result = await databases.CreateDocument(DatabaseId, _collectionId,
            item.GetId() ?? throw new InvalidOperationException(), item.ToJson(true));
        return JsonSerializableBase.FromJson<T>(result.Data.ToJson()) ?? throw new InvalidOperationException();
    }
    catch (Exception e)
    {
        throw new Exception($"Error in Create: {e.Message}");
    }
}
thelongestsigh
9 Dec, 2024, 21:49

Then for example in the SeedDb class, i use it as follows:

thelongestsigh
9 Dec, 2024, 21:49
TypeScript
var createdActivity = await unitOfWork.ActivityService.Create(
    new Activity(100.0, DateTime.Now, "Lunch", null)
);

var updatedActivity = await unitOfWork.ActivityService.Update(
    createdActivity.Id,
    new Activity(100.0, DateTime.Now, "Lunch", createdUsers.ToList())
);

var createdGroup = await unitOfWork.GroupService.Create(new Group("Dev Team"));

await unitOfWork.GroupService.Update(
    createdGroup.Id,
    new Group("Dev Team", createdUsers.ToList(), new List<Activity> { updatedActivity })
);
thelongestsigh
9 Dec, 2024, 21:50

For example my model which i have here is serialized as following:

thelongestsigh
9 Dec, 2024, 21:51
TypeScript
public class Activity : JsonSerializableBase
{
    public Activity(double price, DateTime dateTime, string activityName, List<UserInfo>? userInfos)
    {
        Price = price;
        DateTime = dateTime;
        ActivityName = activityName;
        UserInfos = userInfos;
        Id = string.IsNullOrEmpty(Id) ? ID.Unique() : Id;
    }

    public Activity(double price, DateTime dateTime, string activityName)
    {
        Price = price;
        DateTime = dateTime;
        ActivityName = activityName;
        Id = string.IsNullOrEmpty(Id) ? ID.Unique() : Id;
    }

    public Activity()
    {
        Id = ID.Unique();
    }

    [JsonProperty("Price")] 
    public double Price { get; set; }

    [JsonProperty("DateTime")] 
    public DateTime DateTime { get; set; }

    [JsonProperty("ActivityName")] 
    public string ActivityName { get; set; }

    [JsonProperty("userInfo")] 
    public List<UserInfo>? UserInfos { get; set; }
}
thelongestsigh
9 Dec, 2024, 21:51

Hope that clarifies how i use the sdk in code 🫡

thelongestsigh
9 Dec, 2024, 21:52

And to fully answer your question i have multiple relations as you see in code

Kenny
9 Dec, 2024, 22:12

And you're sure every related item has the permissions set on them? This wouldn't really be an issue with the SDK as that is just a wrapper for the Appwrite REST API.

thelongestsigh
9 Dec, 2024, 22:46

Yes correct but at times the issue is not the permission but it recreates an item that already exists when creating a relation. Altough the ids are the same

Kenny
10 Dec, 2024, 20:58

Hmm, I'm not entirely sure what the issue could be, and I know this does not help your situation right now, but relationships are in beta and pretty inefficient and have issues with them. I would realistically suggest migrating away from using them until they get the revamp they need.

Kenny
10 Dec, 2024, 21:00

Right now, the suggestion for related data is to create a foreign key attribute and utilize that to fetch/update data when necessary. With the next release (1.7) database transactions are coming which should help you when you have, for example, a table of data and you need to fetch a child item for each record.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more