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

Second insert to schema always throws error #3989

Closed
ProfessionalAmateur opened this issue Sep 30, 2021 · 5 comments
Closed

Second insert to schema always throws error #3989

ProfessionalAmateur opened this issue Sep 30, 2021 · 5 comments

Comments

@ProfessionalAmateur
Copy link

ProfessionalAmateur commented Sep 30, 2021

How frequently does the bug occur?

All the time

Description

I am upgrading to Realm 10.8.0 and now when I perform a write/insert to a schema the first insert is successful and the second always throws an error similar to [Error: Workflows.InternalName must be of type 'string', got 'null' (null)] (I am hard coding the values for the insert so value is not null.

Stacktrace & log output

 LOG  Running "Test" with {"rootTag":1}
 LOG  Db: Realm created
 LOG  Realm records: Internal Name0
 LOG  [Error: Workflows.InternalName must be of type 'string', got 'null' (null)]

**Note** Workflows is my schema class, and InternalName is a property on the class.

Can you reproduce the bug?

Yes, always

Reproduction Steps

  1. Created sample React-Native project.
  2. Installed Realm.
  3. Create realm schema, perform two inserts/writes to schema.
  4. First insert is successful (press button to insert)
  5. Second insert fails every time. (press button again)

Package.json

"react": "17.0.2",
"react-native": "0.65.1",    
"realm": "^10.8.0"

Apps.js

import React from 'react';
import { View, Button, StyleSheet } from 'react-native';
import Realm from 'realm';

class Workflows {
  ExternalName;
  InternalName;

  static schema = {
    name: 'Workflows',
    primaryKey: 'InternalName',
    properties: {
      ExternalName: 'string',
      InternalName: 'string',
    }
  };
}

export default class App extends React.Component {
  constructor() {
    super();
    this.realm = new Realm({
      schema: [Workflows],
      schemaVersion: 1,
    });
    console.log("Db: Realm created");
  }

  record;
  ctr = 0;

  saveData = () => {
    let workflows = new Workflows();
    workflows.InternalName = "Internal Name" + this.ctr;
    workflows.ExternalName = "External Name" + this.ctr;
    this.ctr++
    try {
      this.realm.write(() => {
        this.record = this.realm.create(Workflows.schema.name, workflows);
      })
      this.record = this.realm.objects(Workflows.schema.name);
      console.log(`Realm records: ${this.record.map((workflow) => workflow.InternalName)}`);
    }
    catch (error) {
      console.log(error)
    }
  }

  render () {
    return (
      <View style={styles.appContainer} >
        <Button title="Save Realm data" onPress={this.saveData} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  appContainer: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
  }
})### Version

10.8.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Android 7.1.1

Build environment

Which debugger for React Native: ..
No debugger, writing errors to console.

@kneth
Copy link
Contributor

kneth commented Oct 1, 2021

The issue is let workflows = new Workflows();, and I believe it is a duplicate of #3642

@ProfessionalAmateur
Copy link
Author

Thanks, that does look similar. Bummer, this worked on previous releases of Realm. I can clone the object as a workaround so Object.assign(Object.create(Object.getPrototypeOf(Workflows)), Workflows) but to do that everything in the application seems messy.

@kraenhansen
Copy link
Member

kraenhansen commented Oct 4, 2021

I can clone the object as a workaround

You shouldn't have to.
Any reason you can't simply use plain objects to build up the Workflows before creation, like so:

  saveData = () => {
    let workflows = {
      InternalName: "Internal Name" + this.ctr;
      ExternalName: "External Name" + this.ctr;
    };
    this.ctr++
    try {
      this.realm.write(() => {
        this.record = this.realm.create(Workflows.schema.name, workflows);
      })
      this.record = this.realm.objects(Workflows.schema.name);
      console.log(`Realm records: ${this.record.map((workflow) => workflow.InternalName)}`);
    }
    catch (error) {
      console.log(error)
    }
  }

BTW: Assigning this.record both in your write callback and afterwards (result of calling this.realm.objects) seems like a mistake to me.

@ProfessionalAmateur
Copy link
Author

Any reason you can't simply use plain objects to build up the Workflows before creation,

This example above is a simplified example from the real project which uses typescript. We are upgrading from 2.2.8 to 10.8.0 and all these events worked as expected in the earlier release. I wanted to create a dummy app to show only the issue but the actual application we are upgrading uses Realm in many places, with several different schema's for inserts, updates, reads, deletes, etc.... We could try and change all of them to simple Objects but I feel that defeats a bit of the purpose. Just wanted to verify I was not doing something simple incorrectly, or if it was a bug, or if they created breaking changes in Realm between 2.2.8 and 10.8.0 that I need to accommodate for,

@kraenhansen
Copy link
Member

@ProfessionalAmateur calling the constructor of a class-based model has never been officially supported.

Our documentation has pointed to passing plain objects into realm.create instead of instances of the Workflows class in your example. It might not have been as broken in previous versions and I agree that we should have probably errored out in earlier versions letting you know that this was not supported.

Improving the developer experience of class-based models is one of the next things we'll be working on.
As such, I'll close this issue and do my best to remember to update this issue as we release a version of our library with better support for instantiation of model classes.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants