0

I am trying to open a new Realm db file using realm-cpp sdk with the following schema that includes nested objects

struct Card : realm::object<Card> {
    realm::persisted<realm::object_id> _id{realm::object_id::generate()};
    realm::persisted<std::string> name;
    realm::persisted<std::string> question;
    realm::persisted<std::vector<std::string>> options;

    static constexpr auto schema =
        realm::schema("Card",
                      realm::property<&Card::_id, true>("_id"),
                      realm::property<&Card::name>("name"),
                      realm::property<&Card::question>("question"),
                      realm::property<&Card::options>("options")
        );
  };

struct Deck : realm::object<Deck> {
    realm::persisted<realm::object_id> _id{realm::object_id::generate()};
    realm::persisted<std::string> name;
    realm::persisted<std::vector<Card>> cards;

    static constexpr auto schema =
        realm::schema("Deck",
                      realm::property<&Deck::_id, true>("_id"),
                      realm::property<&Deck::name>("name"),
                      realm::property<&Deck::cards>("cards")
        );

  };

auto realm = realm::open<Deck>({dbFile});

If I remove the nested property realm::persisted<std::vector<Card>> cards, everything works as expected. But when I add the nested object, it gives runtime exception:

DB: 30017 Thread 0x108a6c580: Open file: ./test.db/realm.db
libc++abi: terminating with uncaught exception of type realm::SchemaValidationException: Schema validation failed due to the following errors:
- Property 'Deck.cards' of type 'array' has unknown object type 'Card'

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

Could you suggest what could be wrong with my db schema?

Versions:

  • realm-cpp sdk alpha
  • cmake 3.18.1
  • macOS Monterey 12.6.5
  • Apple clang version 14.0.0 (clang-1400.0.29.202). Target: arm64-apple-darwin21.6.0.
matt
  • 33
  • 7
  • 2
    Seems like you need to pass both schemata to open: https://stackoverflow.com/questions/46520171/property-of-type-array-has-unknown-object-type-in-realm-object-server – fabian May 25 '23 at 07:46
  • 1
    @fabian is spot on: realm::open – otso May 25 '23 at 10:51
  • 1
    [Docs here](https://www.mongodb.com/docs/realm/sdk/cpp/model-data/object-models/#realm-schema:~:text=When%20opening%20a%20realm%2C%20you%20must%20specify%20which%20models%20are%20available%20by%20passing%20the%20models%20to%20the%20template%20you%20use%20to%20open%20the%20realm.%20Those%20models%20must%20have%20schemas%2C%20and%20this%20list%20of%20schemas%20becomes%20the%20realm%20schema.). – otso May 25 '23 at 10:58
  • Oh wow, exactly! Thank you so much @fabian ! How could I have missed that... – matt May 25 '23 at 15:03

0 Answers0