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.

Document what overriding field names mean

See original GitHub issue

Goal

Filter table by field names specified with RealmField.

Expected Results

No crashes.

Actual Results

App crashes:

Caused by: java.lang.IllegalArgumentException: Invalid query: field '_id' not found in class 'cards'.
   at io.realm.internal.fields.CachedFieldDescriptor.compileFieldDescription(CachedFieldDescriptor.java:80)
   at io.realm.internal.fields.FieldDescriptor.compileIfNecessary(FieldDescriptor.java:292)
   at io.realm.internal.fields.FieldDescriptor.getColumnIndices(FieldDescriptor.java:185)
   at io.realm.RealmQuery.equalToWithoutThreadValidation(RealmQuery.java:309)
   at io.realm.RealmQuery.in(RealmQuery.java:551)
   at io.realm.RealmQuery.in(RealmQuery.java:531)
   ...

Steps & Code to Reproduce

  1. Create a class with a custom table name and a custom field name.
  2. Try to query for items using field name passed to RealmField.

Code Sample

Constants replaced with values.

@RealmClass(name = "cards")
public class CardModel implements RealmModel {

    @PrimaryKey
    @Required
    @RealmField(name = "_id")
    private String mKey;

    // getters and setters
}

...

try (final Realm realm = mRealmProvider.get()) {
	final RealmResults<CardModel> all1 = realm.where(CardModel.class)
			.in("mKey", map(keys)) // will NOT crash here
			.findAll();
	final RealmResults<CardModel> all2 = realm.where(CardModel.class)
			.in("_id", map(keys)) // will crash here
			.findAll();
}

Version of Realm and tooling

Realm version(s): 5.0.0

Realm sync feature enabled: no

Android Studio version: 3.0.1

Which Android version and device: API 24, Emulator

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
mlykotomcommented, Sep 19, 2018

If it is because you are in the middle of a refactoring searching for the string "mKey" will do the trick (but of course you need to check it isn’t mKey from another class).

This is also why we in a lot of internal tests have the best practice of static fields in the model class:

public class Person extends RealmObject {
  public static final FIELD_NAME = "name";
  public String name;
}

realm.where(Person.class).equalTo(Person.FIELD_NAME, "Jane").findAll();

This is exactly the reason why it should be used in typed queries as well, because when you (accidentally) rename the java field name to e.g. firstName, you’ll still have the proper FIELD_NAME for realm queries.

If you have it this way, then there’s no IDE help and you may easily end up having FIELD_NAME different than java name.


Also another use case for using it in queries is, that then you can have (in kotlin) something like this:

const val TYPE_FIELD = "type"

@RealmField(TYPE_FIELD)
private var _type: String

var type: TypeEnum
  get() = _type.toString()
  set(value) {
     _type = TypeEnum.valueOf(value)
  }

And you can still use:

realm
  .where<MyClass>()
  .equals(TYPE_FIELD, TypeEnum.TYPE_A)
  .findAll() 

Which then provides more type-safety, because from code you may access whatever name you added, but in queries you use fields you defined in annotation.

0reactions
cmelchiorcommented, Mar 22, 2018

Yes, I can see the problem. Honestly, I think a better solution to your problem would be if we started allowing non-polymorphic inheritance, which would allow for a base class with shared fields. So far we have not done that because we feared that not having polymorphism would really trip people up but both our Cocoa binding and ObjectBox do this, so we should probably revisit that decision.

The reason I’m hesitant about “just” allowing queries on both internal and Java names is that it muddles the layers between Java defined names and internal names and we need to enforce that you cannot use an internal name that is already being used in the Java model class.

I’ll need to think a little about this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding field names for Description, Value and Flag fields
Overriding names are displayed instead of the default name in table columns, labels, and in the Formula Builder. The overriding names for ...
Read more >
Document Customization - Overrides Tab Page - Xytech Systems
This tab page allows you to define a variety of overriding custom properties for standard fields in the table selected on the DOCUMENT...
Read more >
Overriding Salesforce Field Metadata - Salesforce Help
You can override the field metadata that the sfdcDigest transformation extracts from a Salesforce object to make the data appear differently in a...
Read more >
Configure field overrides | Grafana documentation
Overrides allow you to customize visualization settings for specific fields or series. This is accomplished by adding an override rule that targets a...
Read more >
Understand Field Type Detection and Naming Improvements
Field name clean-up ... Field names that contain specific characters or capitalized in a certain way are renamed. Field values that include square...
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