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.

handling whitespaces in column names

See original GitHub issue

I can’t find anything in documentations about how to filter on columns with whitespace in names.

Goals

do filtering on fields containing whitespaces.

Actual Results

Invalid predicate. exception

Code Sample

realm.objects('course').filtered('lessons list.@size == 2');

Version of Realm and Tooling

  • Node or React Native: ? Node

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kraenhansencommented, Dec 19, 2019

@noorzaie

But your UI tool opens the Realm right?

Using mapTo will not change the schema. Instead it adds an alias available only for your client while the Realm is opened. It could be a transformation of the name, that doesn’t contain a whitespace such that it can be used in queries.

You could:

  1. Open the Realm to read its schema dynamically
  2. Go through the object schemas properties and transform all the names in the properties into camelCase, storing them as mapTo for each of the properties.
  3. Close the Realm and reopen it with the modified schema containing mapTos (which will not make any changes to the schema of the Realm file).
  4. Query using the mapTo property names instead of the ones containing whitespaces.

Example implementation

function openRealmMapped(config) {
  const realm = new Realm(config);
  const schema = realm.schema;
  realm.close();
  // Loop the object schemas and their properties
  for (const objectSchema of schema) {
    for (const [ name, property ] of Object.entries(objectSchema.properties)) {
      property.mapTo = camelCased(name);
    }
  }
  // Reopen with the modified schema, which does not change the schema store in the Realm file
  return new Realm({ ...config, schema });
}

Somewhere else …

const realm = openRealmMapped({ path: 'whatever.realm' });
console.log(realm.schema);
// [ { name: "User", properties: { "first name": { type: "string", mapTo: "firstName" } } } ]
const john = realm.objects("User").filtered("firstName = 'John'")[0];

This might contain errors - as I havn’t actually run it, but it should demonstrate the idea - does it? 😃

1reaction
noorzaiecommented, Dec 20, 2019

@kraenhansen That sounds great. Thanks so much for explanation and sample code. I’ll try it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deal with white spaces in Column Names of your Raw ...
I have a raw file in my AWS S3 bucket. I have mounted the S3 bucket with my Databricks instance, that's how I...
Read more >
How to write SQL queries with spaces in column names
In this article, we are going to learn how we can write a SQL query with space in the column name. Blanks spaces...
Read more >
How to remove trailing whitespaces from column headers in ...
In this blog post we'll do a small thing to remove trailing or leading whitespaces from a DataFrame's column names.
Read more >
How can I strip the whitespace from Pandas DataFrame ...
This is where my mind went since I like to strip whitespace earlier in my process flow and handle incoming data with variable...
Read more >
Remove spaces from column names in Pandas - GeeksforGeeks
Removing spaces from column names in pandas is not very hard we easily remove spaces from column names in pandas using replace() function....
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