Realm is Saving Null Primary Key


#1

Hello Folks, I am building a xamarin.forms project and I’ve created my models as RealmObject, as below

 public class SalePlace : RealmObject
    {
        [PrimaryKey]
        public string Id { get; set; } = Guid.NewGuid().ToString();

But when I add it to Realm and it is replicated to the realtime sync on Realm Object Server the ID is saved as Null as image below

If I remove the PrimaryKey annotations I am able to save the the ID, when I set the Primary key I get null .

I would be glad if someone have any hint for me .

Thanks in Advance.

Pellizzoni


#2

This is the first time I’m seeing something like this. Can you post an example of how you’re adding items to the Realm? Keep in mind that because the setter is public, it’s possible someone still sets the value to null before the object gets added to the Realm.


#3

This is how I am adding. I will try to change to private set

   _realm.Write(() =>
            {
                _realm.Add(salePlace);
            });

#4

Well, I tried the private set, and yet, the same issue, :disappointed:


#5

Looking at it again, it’s very surprising as the primary key is supposed to be unique so when you add a second object with a null Id, you should get a runtime exception. Do you think you can share a small repro project so I can debug it?


#6

Well This is what I can Share

MODEL

public class SalePlace : RealmObject
    {
        [PrimaryKey]
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string Name { get; set; }
        public string ContactName { get; set; }
        public Address Address { get; set; }
        public string Image { get; set; }
        public string Telephone { get; set; }
        public string Mobile { get; set; }
    }
public class Address : RealmObject
    {
        [PrimaryKey]
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string Street { get; set; }
        public string Number { get; set; }
        public string Additional { get; set; }
        public string Neighborhood { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        //public string GeoLocation { get; set; } it can be Position

    }

Realm Service to Save

 public class RealmService : IDatabaseService
 {
        private Realm _realm;
        private string _authURL = "URL_AUTH";
        private string _realmURL = "URL_REALM";

        private void GetConfiguration()
        {
            var user = User.GetLoggedInUser(Settings.RealmIdentity, new Uri(_authURL));
            if (user != null)
            {
                var prefix = Settings.UserEmail.Split('@')[0];
                var configuration = new SyncConfiguration(user, new Uri(_realmURL + "/~/" + prefix));
                _realm = Realm.GetInstance(configuration);
            }
        }
       public void SavePlace(Address address, SalePlace salePlace)
        {
            if (_realm == null) GetConfiguration();
            _realm.Write(() =>
            {
                salePlace.Address = address;
                _realm.Add(address);
                _realm.Add(salePlace);
            });
        }
        public void Update(Address address, SalePlace salePlace)
        {
            if (_realm == null) GetConfiguration();
            _realm.Write(() =>
            {
                _realm.Add(address, true);
                _realm.Add(salePlace, true);
            }); ;
        }
}

The address and salePlace object are instanciated on the ViewModel through PropertyBinding, then I call the service to save or update, and I made a simple test to update a value with an Id defined and then I got the value duplicated.

And this was a Simple Update.


#7

Hi @nirinchev do you think this could be related to a .NET Standard 2.0 Issue?
regards


#8

We are using

[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();

in a .NET Standard 2.0 class library with Realm and it correctly inserts guids as primary keys…


#9

I have not been able to reproduce that with the information provided and we have a lot of integration tests that validate this works and none of them have failed even sporadically, so I’m guessing there must be something peculiar we’re not seeing here.


#10

Let me ask you, which version of realm are you using ? Because, I had to use a workaround make realm works on my project.

I had to follow these steps here to get my project working on version 1.6.0, and now I am able to reproduce the error in a new project

I am also using Visual Studio 2017

@nirinchev here is GitHub Link

Thanks in Advance.


#11

I’ve added a project on github on the thread where I could reproduce the error.


#12

We’re using Realm Cloud. Preciously we used Realm Database with the same method for setting primary keys.


#13

Yet this isnt working for me, I’ve created fresh and simple projects, however, I havent any success

=(


#14

The problem is caused by using Realm .NET 1.6. Please upgrade to the latest version (3.0.0) and things should start working for you.