question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to create relationships on non id fields is not clear

See original GitHub issue

Since Watermelon auto assigns the id field, I’m trying create a relationship between two models on non ‘id’ fields as per their relationship on the remote resource. the example would be:

class Pledge extends Model {
  static table = 'pledges'
  static associations = {
    profiles: { type: 'belongs_to', key: 'id_profile' },
  }

  @field('pledge_id') pledgeId
  @field('owner') owner
  @field('amount') amount

  @relation('profiles', 'id_profile') profile

}

class Profile extends Model {
  static table = 'profiles'
  static associations = {
    pledges: { type: 'has_many', foreignKey: 'id_profile' }
  }

  @field('addr') addr
  @field('type') type
  @field('name') name
  @field('id_profile') idProfile

  @children('pledges') pledges

When I try to create the Pledge on pledge.profile.set(profile.id) the profile is not set and id_profile is “0” when it should have a value.

So this is two questions.

  1. Would this be the ideal way to model this relationship in Watermelon?
  2. Is this the correct way to create the Pledge record?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bgitscommented, Feb 4, 2019

@bgits All right, so let me get this straight:

you have a Pledge record, which has its id (which is a local-only Watermelon ID), and its pledgeId (which is what the remote server assigns to it). And analogous with Profile. Correct?

This is kind of a problem for Watermelon at this moment, since Watermelon assumes all relation IDs are to id fields — and that local and remote IDs are consistent.

See this issue for the discussion on how to deal with local and remote IDs: #216 — if you can, for example, normalize IDs to all be local IDs during synchronization, all will be good.

Another solution (at least a partial one) is to add a local id field to relation/immutableRelation decorator. See @esbenp’s thread here: #219 — this is an analogous situation. If you’d like to contribute to that, that would be great.

Let me know if this clears up the situation and what you choose to do in your case 😃

For now I resorted to creating these relationships on the id fields during the data import and keeping their remote id’s on {model}Id field. I will see if this degrades into something problematic for me.

0reactions
KrisLaucommented, Oct 8, 2021

@radex Is it still not possible to create a relation on non _id fields?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create, edit or delete a relationship - Microsoft Support
The Edit Relationships dialog box appears. Select the Enforce Referential Integrity check box. Select either the Cascade Update Related Fields or the Cascade...
Read more >
Enforcing Referential Integrity on Table Relationships - YouTube
... relationships between two tables in Microsoft Access using t... ... the Cascade Update Related Fields and Cascade Delete Related Records ...
Read more >
Object Relationships Overview - Salesforce Help
Clearing the field is a good choice when the field doesn't have to contain a value from the associated lookup record.
Read more >
Single-criteria relationships - FileMaker Pro
In the Contacts table, the Customer ID is a simple number field, so do not set any auto-entry options, but set the field...
Read more >
The 6 Types of Relationships in Salesforce
Lookup fields are not required on the page layout of the detail record but if you make them a required field, it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found