sqlite_orm
sqlite_orm copied to clipboard
Primary key in column does not work
I have following code. struct Data { int mDefault; /**< 0=User or 1=Default*/ std::string mLang1; };
return make_storage(DBPath,
make_table("UserPreferences",
make_column("IsDefault", &Data::mDefault, primary_key()),
make_column("Lang1", &Data::mLang1)
)
);
- The created database does not create primary key.
- Is there any way to get Data object for specific primary key without using getall function like storage.get
(insertedId);? Does storage.get (insertedId); work in this case?
Hi. Thanks for using the lib. Yes, you can get object by primary key with get crud member function.
auto storage = make_storage(DBPath,
make_table("UserPreferences",
make_column("IsDefault", &Data::mDefault, primary_key()),
make_column("Lang1", &Data::mLang1)));
storage.sync_schema(); // this is optional - call it if you don't have db file at given path - this call will create it with tables you specified in `make_storage` call.
Data data1{ -1, "en" };
auto insertedId = data1.mDefault = storage.insert(data1); // `insert` function inserts all data from a given object except primary_key'ed column and returns a new primary_key. If you want to insert with id call `storage.replace` instead
auto insertedData = storage.get(insertedId);
// now insertedData and data1 are the same
I am not able reproduce it now. If I get same issue again, I shall reopen it.