Error when using push to add objects to Realm.List

I’m using @realm/react 0.6.2 and encountering an odd error. I create/push the same way using realm 11.10.1 and don’t get this error and I don’t see anywhere in the docs where it talks about actually adding new objects to a Realm.List. When I try to add an object to a Realm.List with a one-to-many relationship, it works if I just do it once, but if it’s in a loop I’m getting this error:

Attempting to create an object of type 'Photo' with an existing primary key value '655e5091fb680de919de38cb'.

Here is a simplified version of my models:

export class ArcReview extends Realm.Object {
  _id!: BSON.ObjectId;
  files!: Realm.List<File>;

  static schema: ObjectSchema = {
    name: 'ArcReview',
    properties: {
      _id: {type: 'objectId', default: new BSON.ObjectId()},
      photos: 'Photo[]',
    },
    primaryKey: '_id',
  };
}

export class Photo extends Realm.Object {
  _id!: BSON.ObjectId;
  url!: string;
  thumbUrl!: string;

  static schema: ObjectSchema = {
    name: 'Photo',
    properties: {
      _id: {type: 'objectId', default: new BSON.ObjectId()},
      url: 'string',
      thumbUrl: 'string',
    },
    primaryKey: '_id',
  };
}

My code for the write transaction is:

realm.write(() => {
      let newArcReview = realm.create<ArcReview>(ArcReview, {});
      photos.forEach((photo) => {
        let url = photo.picture;
        let thumbUrl = photo.thumb;
        let newPhoto = realm.create<Photo>(Photo, {url, thumbUrl});
        newArcReview.photos.push(newPhoto);
      });
    });

Anyone have any ideas on how/why this is not working?

Hi @Kurt_Libby1, since your default value in your schema for the primary key is the same each time you create an object, you’ll get that error. To generate a new ObjectId each time, replace your current value with an anonymous function:

default: () => new BSON.ObjectId()