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

Update object relationship is trying to create it again #6322

Closed
mMarcos208 opened this issue Dec 14, 2023 · 11 comments
Closed

Update object relationship is trying to create it again #6322

mMarcos208 opened this issue Dec 14, 2023 · 11 comments
Assignees
Labels
Encryption:Off Frequency:Always More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Repro:Always SDK-Use:Local T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@mMarcos208
Copy link

How frequently does the bug occur?

Always

Description

I'm trying to update a relationship.

const realm = this.getRealm()
const [user] = realm.objects<User>(User.schema.name)

user.currentFarm = newCurrentFarm

[Error: Attempting to create an object of type 'Farm' with an existing primary key value '2311'.]

Realm 11.9.0 works well.

Stacktrace & log output

[Error: Attempting to create an object of type 'Farm' with an existing primary key value '2311'.]

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

12.3.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android

Build environment

Flipper

"react-native": "0.72.7",

Cocoapods version

No response

@kneth
Copy link
Member

kneth commented Dec 18, 2023

@mMarcos208

In order to understand the issue better, can you provide the schema for User and Farm. Moreover, it would be good to know the properties of currentFarm and newCurrentFarm.

I am speculating if is related to #6239. Can you downgrade to v12.3.0 and see if you get the same error?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Dec 18, 2023
@mMarcos208
Copy link
Author

@kneth My code.

import {ObjectSchema} from 'realm'

import {Farm} from './Farm'

export class User {
  id!: number

  // current farm id
  currentFarm?: Farm

  static schema: ObjectSchema = {
    name: 'User',
    primaryKey: 'id',
    properties: {
      id: 'int',
      currentFarm: 'Farm?',
    },
  }
}

import Realm from 'realm'

export class Farm {
  id!: number

  static schema: Realm.ObjectSchema = {
    name: 'Farm',
    primaryKey: 'id',
    properties: {
      id: 'int',
    },
  }
}

Downgrade my version to 12.3.0 and I get the same issue.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Dec 19, 2023
kneth added a commit that referenced this issue Dec 20, 2023
@kneth
Copy link
Member

kneth commented Dec 20, 2023

@mMarcos208 I am trying to reproduce the observed behavior in #6330. How does it match your code?

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Dec 20, 2023
@mMarcos208
Copy link
Author

@kneth try update Farmer with existing id again.

For exemple:


const farmer = this.realm.objectForPrimaryKey("Farmer", 11);
const farm = this.realm.objectForPrimaryKey("Farm", 22);

this.realm.write(() => {
farmer.currentFarm = farm
}

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Dec 20, 2023
@kneth
Copy link
Member

kneth commented Dec 20, 2023

@mMarcos208 I have added a test like that, and it seems to work,

@mMarcos208
Copy link
Author

mMarcos208 commented Dec 21, 2023

@kneth can you test this cenary?

it("Update - same object", function (this: RealmContext) {
  this.realm.write(() => {
    this.realm.create("Farmer", { id: 11, currentFarm: { id: 22 } });
  });

  this.realm.write(() => {
    const newFarm = this.realm.create(Farm, { id: 33 });
    //@ts-expect-error cannot be null
    farmer.currentFarm = newFarm;
  });

  const farmer = this.realm.objectForPrimaryKey<Farmer>(Farmer, 11);
  const oldFarm = this.realm.objectForPrimaryKey<Farm>(Farm, 22);

  this.realm.write(() => {
    //@ts-expect-error cannot be null
    farmer.currentFarm = oldFarm;
  });

  expect(this.realm.objects<Farmer>(Farmer).length).equals(1);
  expect(this.realm.objects<Farm>(Farm).length).equals(2);
  expect(this.realm.objects<Farmer>(Farmer).currentFarm.id).equals(22);

}); 

@kneth
Copy link
Member

kneth commented Dec 21, 2023

@mMarcos208 I have added the suggested test, and the test passes.

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Dec 21, 2023
@kneth
Copy link
Member

kneth commented Jan 3, 2024

As we haven't been able to reproduce it, I must ask you to create a unit test (like I did in #6330) to reproduce the observed behavior.

@mMarcos208
Copy link
Author

mMarcos208 commented Mar 13, 2024

Migration with same error. I change my schema:

I'm using realm 12.6.2

from

import {ObjectSchema} from 'realm'

export class Mapa {
  id!: number
  areaFazendaId!: number
 
  static schema: ObjectSchema = {
    name: 'Mapa',
    primaryKey: 'id',
    properties: {
      id: 'int',
      areaFazendaId: 'int'
    },
  }
}

to

import {ObjectSchema} from 'realm'

import {AreaFazenda} from '.'

export class Mapa {
  id!: number
  areaFazenda?: AreaFazenda
 
  static schema: ObjectSchema = {
    name: 'Mapa',
    primaryKey: 'id',
    properties: {
      id: 'int',
      areaFazenda: 'AreaFazenda?'
    },
  }
}

Remove areaFazendaId and add object AreaFazenda cause same error.

[Error: Attempting to create an object of type 'AreaFazenda' with an existing primary key value '60912'.]

My migration

  if (oldRealm.schemaVersion < 21 && newRealm.schemaVersion === 21) {
    const areasKeyById = keyBy(oldRealm.objects<schemas.AreaFazenda>(schemas.AreaFazenda.schema.name), 'id')
    const oldsMaps = oldRealm.objects<schemas.Mapa>(schemas.Mapa.schema.name)
    const newMaps = newRealm.objects<schemas.Mapa>(schemas.Mapa.schema.name)

    oldsMaps.forEach((x, index) => {
      console.log('line error', index)
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      //@ts-ignore
      newMaps[index].areaFazenda = areasKeyById[x.areaFazendaId]
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      delete x.areaFazendaId
    })
  }

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Mar 13, 2024
bimusiek pushed a commit to bimusiek/realm-js that referenced this issue Mar 14, 2024
@nirinchev
Copy link
Member

As @kneth mentioned, if you have a project that we can run and repro (not just code snippets we have to assemble ourselves), please share it with us.

@nirinchev nirinchev added More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. and removed Needs-Attention Reporter has responded. Review comment. labels Mar 20, 2024
@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Mar 20, 2024
Copy link
Contributor

github-actions bot commented Apr 4, 2024

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@github-actions github-actions bot closed this as completed Apr 4, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Encryption:Off Frequency:Always More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Repro:Always SDK-Use:Local T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

3 participants