relationship loss when updating object realm database

I have an Android application where there is a week table and a visit table. The visits table has a 1:n relationship with the week table. In this way, the visit has a reference to the week through an object. However, when updating the week, the visit table loses the week reference. I do not change the primary key, I only update the other fields. What could be happening?

JsonObject semanaJson = semanasArray.get(auxI).getAsJsonObject();

                                Semana semana = (Semana) semanaRepo.getById(semanaJson.get("id").getAsInt());

                                if (semana == null) {
                                    semana = semanaRepo.getObject(semanaJson);
                                }

                                semana.mergeWithJSON(semanaJson);
public void mergeWithJSON(JsonObject jsonObject) {
		@SuppressLint("SimpleDateFormat")
		SimpleDateFormat formatter = new SimpleDateFormat(DateFormats.DATA);

		setNome(jsonObject.get("nome").getAsString());
		try {
			setDiaInicial(formatter.parse(jsonObject.get("data_inicio").getAsString()));
		} catch (ParseException e) {
			Sentry.captureException(e);
			throw new RuntimeException(e);
		}
		try {
			setDiaFinal(formatter.parse(jsonObject.get("data_fim").getAsString()));
		} catch (ParseException e) {
			Sentry.captureException(e);
			throw new RuntimeException(e);
		}

		Date dataInicio = getDiaInicial();
		Date dataFinal = getDiaFinal();
		Calendar cal = Calendar.getInstance();

		cal.setTime(dataInicio);
		cal.set(Calendar.HOUR_OF_DAY, 00);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
		setDiaInicial(cal.getTime());

		cal.setTime(dataFinal);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		setDiaFinal(cal.getTime());
	}

For a lot of ORM systems (Room, or others), when you update an entity, it’s important that the entity’s identity (like the primary key) remains the same and that the entity is not detached from its associated objects.

If the ORM detects that an object has been replaced or removed, it might also remove the associated references.

To me, it’s looking like the Semana object is being detached from the persistence context, and another instance is being created and persisted. The update process might be removing the original Semana object from the context and adding an another one which is causing the references from Visit objects to become invalid.

I’d double check and make sure semanaRepo.update isn’t trying to create new objects, and that you ar using “merge” ‘Update’ and ‘save’ appropriately without changing the identity. You also should double check and make sure that ‘semana’ and 'Visit" are also maintaining their relationship through the entire process.