Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't retrieve data through RealtimeChannel.OnUpdate #22

Closed
Shenrak opened this issue Feb 6, 2023 · 10 comments
Closed

Can't retrieve data through RealtimeChannel.OnUpdate #22

Shenrak opened this issue Feb 6, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@Shenrak
Copy link

Shenrak commented Feb 6, 2023

Bug report

Describe the bug

RealtimeChannel's response isn't parsed correctly.
Both RealtimeChannel.Model<T>() and PostgresChangesEventArgs.Response.Payload.Data.Record.Record are defaults values.

To Reproduce

Subscribe to a table with RealtimeChannel.OnUpdate

Expected behavior

Getting correct data that's been updated

Screenshots

Table maintenance
image

    [Table("maintenance")]
    public class SchemaMaintenance : BaseModel
    {
        [PrimaryKey("forced_version", false)] public string ForcedVersion { get; set; }
        [Column("is_on_maintenance")] public bool IsOnMaintenance { get; set; }
    }
    public static void StartConnectedDeviceWatcher()
    {
        var channel = SupabaseManager.app.Realtime.Channel("realtime", "public", "maintenance");

        channel.OnUpdate += (object sender, PostgresChangesEventArgs a) =>
        {
            var m = a.Response.Model<SchemaMaintenance>();
            Console.WriteLine("Item updated: " + a.Response.Payload.Data.Record.Record);
        };
        channel.Subscribe().ContinueWith(HandleErrors);
    }

The following screenshot showcases what's happening in RealtimeChannel.Model()
image

The raw content of Json :
image

System information

  • OS: Windows (Unity)

Additional context

I'd like to add that it was not clear to me as what to put in databasefield of Channel() :
image
As my table was in the postgres database, i had a though time to figure it out :p

Guillaume

@Shenrak Shenrak added the bug Something isn't working label Feb 6, 2023
@Shenrak
Copy link
Author

Shenrak commented Feb 6, 2023

columns and commit_timestamp a correctly populated

@Shenrak
Copy link
Author

Shenrak commented Feb 6, 2023

Managed to get it working by adding :

[JsonProperty("forced_version")]
[JsonProperty("is_on_maintenance")]

as so :

    [Table("maintenance")]
    public class SchemaMaintenance : BaseModel
    {
        [JsonProperty("forced_version")][PrimaryKey("forced_version", false)] public string ForcedVersion { get; set; }
        [JsonProperty("is_on_maintenance")][Column("is_on_maintenance")] public bool IsOnMaintenance { get; set; }
    }

Might be a good idea to embed it in [Column] and [PrimaryKey] ??

However, seems like old_record is always missing the boolean property is_on_maintenance, has it something to do with it begin a boolean ?
It is missing in all cases, with it being the modified value or not :
image

@acupofjose
Copy link
Collaborator

@Shenrak such a thorough issue! I love when so much data is provided.

I think this is a regression from updating the Newtonsoft.Json dependency - I'll get it pushed out in an update this evening! Thanks so much for digging into it!

@acupofjose
Copy link
Collaborator

Available in [email protected] / [email protected]!

@Shenrak
Copy link
Author

Shenrak commented Feb 7, 2023

Hi,

Made the update, works like a charm !
However, i'm still running into this issue :
image

old_payload is always lacking data, and OnUpdate is always called, even if the UPDATE didn't change anything
Do you have an idea on the matter ?

@acupofjose
Copy link
Collaborator

acupofjose commented Feb 8, 2023

Have you tried using OldModel<T>()? Like so:

var m = a.Response.OldModel<SchemaMaintenance>();

@acupofjose
Copy link
Collaborator

And to be clear... you're having OnUpdate being called on inserts and deletes?

@Shenrak
Copy link
Author

Shenrak commented Feb 9, 2023

I'm using OnUpdate only with updates, i'v not try it with Inserts and Deletes
I might not have been really clear, i'll try to :

        void OnChannelUpdate(object sender, PostgresChangesEventArgs a)
        {
            var oldModel = a.Response.OldModel<SupabaseModel>();
            var model = a.Response.Model<SupabaseModel>();
            if (JsonConvert.SerializeObject(oldModel) != JsonConvert.SerializeObject(model))
            {
                OnRemotelyUpdated?.Invoke(model);
            }
        }

Pretty much what I'm trying to do is to call OnRemotelyUpdated only if the row has effectively been updated.
It happens that a row has been updated but no data changed, because i'm doing bluk upserts of lists with some elements that havn't changed.

The issue here is that OldModel is missing some data, as i linked in my preview message

@acupofjose
Copy link
Collaborator

Gotcha! If you want to receive the "previous" data for updates and deletes, you will need to set REPLICA IDENTITY to FULL, like this: ALTER TABLE your_table REPLICA IDENTITY FULL - you can set that in your supabase console using the SQL editor. (That documentation is here - but I'll add it to the docs in the code as well!)

As for the updates with no changed data being sent - you are calling with an insert, even if there isn't actually a change. So the client is working as expected - you'll need to do the filtering on your side.

acupofjose added a commit that referenced this issue Feb 9, 2023
@Shenrak
Copy link
Author

Shenrak commented Feb 9, 2023

Yup, that's pretty much what I ended up doing, also i don't update the DB if there's been no change anymore
Thanks for your help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants