Realm PostgreSQL Adapter and Composite Primary Keys


#1

Does the Realm PostgreSQL Adapter have a way of handling composite primary keys? I noticed in the documentation for the MSSQL adapter there is a property you can set for them, but the same is not present in the PostgreSQL adapter. If there’s not an easy fix like that, has anyone had any success synchronizing a PostgreSQL with a composite primary key with Realm, and willing to share some tips? Thanks!


#2

@kalex Unfortunately, the PG adapter does not support compound keys at this time.


#3

Ah, that is unfortunate. Any recommendations on workarounds then? I’m thinking probably our best route would be a column that concatenates primary key values or moving to a UUID.


#4

Not really - Some things won’t work if you set it up like that. Or if you only used one of the columns as a key in Realm. For example, if a realm object is deleted, how do we know which Postgres row to delete? Usually we delete by primary key, so if you denote the PK as only one of the Postgres compound keys, what will end up happening is all of the Postgres rows with that value will be deleted. The adapter needs to know what the compound keys are so that it can map the Realm PK to that combination of fields so that it can delete the right row in Postgres.


#5

We don’t have our postgres database set in stone for this project yet, so moving the postgres table to a UUID for its primary key instead of the composite key is not the end of the world.

I’m intrigued by your stating “The adapter needs to know what the compound keys are” statement, though. Is my understanding correct by interpreting that as the adapter needs to know, but we currently don’t have a way to inform it?


#6

Correct - this feature is not built for the PG adapter


#7

A somewhat off-topic question, if you don’t mind (I can move it to a new thread if need be):

Does the PostgreSQL adapter currently support mapping functions for PostgreSQL columns to Realm properties, and vice-versa? The functionality I’m looking for is similar to the mapRealmPropertyName and mapSQLServerColumnName functions from the MSSQL adapter.

I’m trying to get Realm property names to map from camel case names to underscored names currently, and setting mapRealmPropertyName in my adapter.js for the PostgreSQL adapter is not behaving as I expected.

Thanks for all your help!


#8

@kalex yes
https://docs.realm.io/sync/backend-integration/postgres-connector#mapping-postgres-columns-to-realm-models


#9

Ah, yeah, discovered that I had neglected to add my mapping functions to adapter.js. Such a simple mistake, sorry for the trouble.