[odb-users] "Access violation reading location" when querying a view

Seiler Matthias SIGCH Matthias.seiler at stadlerrail.com
Tue May 13 09:37:56 EDT 2025


Hi Boris,

I have the following view ...

```
#pragma db view query(                  \
    "SELECT TpEntity.Id "               \
    "FROM TpEntity "                    \
    "INNER JOIN Switch "                \
    "ON Switch.TpEntity = TpEntity.Id " \
    "WHERE (?)")
struct TpEntityIdBySwitch {
  unsigned long tpEntityId;
};
```

... which is queried like this:

```
transaction t(db->begin());
auto q = "Switch.Id='" + switchId + "'";
auto viewRes = *db->query_one<TpEntityIdBySwitch>(q);
t.commit();
```

Thus, given a `Switch.Id` (whose table has a foreign key to `TpEntity` on `Switch.TpEntity`), I want to get the value of `TpEntity.Id`. It works fine if the passed ID exists. However, if it does not, I get an "Access violation reading location ...", which I'm not able to catch. How can I go around this issue such that it does not crash, maybe getting "some" result (e.g., NULL or optional) regardless of if the table contains the ID?

Kind regards,
Matthias



More information about the odb-users mailing list