sorosh_sabz
sorosh_sabz

Reputation: 3003

How to delete an entry in SQLite ORM with a composite key

I am using the SQLite ORM library for using SQLite in my program and I have a table like below:

auto storage = make_storage("test_remove.sqlite",
                            make_table("objects",
                                       make_column("key_part_1",
                                                   &Object::key_part_1),
                                       make_column("key_part_2",
                                                   &Object::key_part_2),
                                       make_column("name",
                                                   &Object::name),
                                       primary_key(&Object::key_part_1, &Object::key_part_2)));

That, as you can see, this table has a composite key (key_part_1 and key_part_2).

I see the remove wiki page, and, as you can see, there isn't any explicitly documentation about removing composite key objects, but it is saying if you want to remove an object with custom where conditions, use remove_all instead of remove.

How can I remove an object from this table effectively? Is there a better way that uses remove_all?

Upvotes: 2

Views: 259

Answers (1)

sorosh_sabz
sorosh_sabz

Reputation: 3003

In the new SQLite ORM development branch, the creator of this library released a new API that is like below:

remove(Ids... ids)

If you have an object like below:

struct Object {
    int id;
    std::string name;
};

And have a composite key like below:

auto storage = make_storage("",
                            make_table("objects",
                                       make_column("id", &Object::id),
                                       make_column("name", &Object::name),
                                       primary_key(&Object::id, &Object::name)));

With this API, you can remove an composite key object like below:

        storage.remove<Object>(1, "Skillet");

Upvotes: 1

Related Questions