sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Composite key

Open ElephantInTheRoomFirst opened this issue 9 years ago • 23 comments

I want to use composite key which consists of 2 columns. Is there any way to specify in table creation? If yes, please give me syntax of the command.

I wish you happy weekend.

ElephantInTheRoomFirst avatar May 28 '17 10:05 ElephantInTheRoomFirst

Hi. Thanks. Composite key isn't supported yet. It will be added soon. The syntax is assumed to be like this:

make_table("users",
    make_column("id", &User::id),
    make_column("name", &User::name),
    primary_key(&User::id, &User::name));

Have a nice weekend too.

fnc12 avatar May 28 '17 10:05 fnc12

Is it still on your list?

ElephantInTheRoomFirst avatar Jun 16 '17 08:06 ElephantInTheRoomFirst

Yes. Working on it

fnc12 avatar Jun 17 '17 16:06 fnc12

This thing is too complicated - I need to think more. The thing is that this form

make_table("users",
    make_column("id", &User::id),
    make_column("name", &User::name),
    primary_key(&User::id, &User::name));

is kind of difficult cause make_table expects columns and nothing else as arguments. Please tell me what do you expect from composite key feature in sqlite_orm?

fnc12 avatar Jun 21 '17 05:06 fnc12

Good morning, I want use it make sure that there is only one row with given composite key combination is present. Methods

  1. get(composite_key)
  2. remove(composite_key)
  3. is_present(composite_key) //if possible

ElephantInTheRoomFirst avatar Jun 21 '17 05:06 ElephantInTheRoomFirst

Ok I see. I guess that I need to get typename template type of primary key by columns passed as parameter pack in get function. Now get function works another way - it allows you to pass any type and serializes it to sqlite expression. If I change any type to primary key type it will be better cause get won't throw an exception if user tries to get an object with no PRIMARY KEY but won't compile at all cause primary key type will be deduced from columns' types. But what I need is parameter pack filtering. I know how to transform parameter pack, it's easy. But idk how to filter it. Once I know how to perform it I'll go on with the issue.

fnc12 avatar Jun 21 '17 10:06 fnc12

When it will be released?

ElephantInTheRoomFirst avatar Jul 17 '17 07:07 ElephantInTheRoomFirst

in a few weeks. Sorry for a long development time

fnc12 avatar Jul 17 '17 07:07 fnc12

Is it still in your ToDo?

ElephantInTheRoomFirst avatar Aug 28 '17 08:08 ElephantInTheRoomFirst

@Ninja-007 Yes. I got a lot of primary work and that's why it takes long time for me to complete the issue. I'm going to close it in about 2 weeks

fnc12 avatar Aug 28 '17 09:08 fnc12

@Ninja-007 working on it right now. I have get and get_no_throw implemented already

fnc12 avatar Sep 15 '17 16:09 fnc12

@Ninja-007 please check out get, get_no_throw and replace. insert also works but it ignores composite key and works just like replace.

fnc12 avatar Sep 16 '17 11:09 fnc12

Thanks, did you add examples?

ElephantInTheRoomFirst avatar Sep 21 '17 13:09 ElephantInTheRoomFirst

No cause insert works in a way it mustn't work. But you can use it already

auto storage = make_storage("",
                                    make_table("users",
                                               make_column("id",
                                                           &User::id),
                                               make_column("first_name",
                                                           &User::firstName),
                                               make_column("last_name",
                                                           &User::lastName),
                                               primary_key(&User::id, &User::firstName)),
                                    make_table("user_types",
                                               make_column("id",
                                                           &UserType::id,
                                                           primary_key())));
        storage.sync_schema();
        
        storage.replace(User{
            1,
            "Bebe",
            "Rexha",
        });
        auto bebeRexha = storage.get<User>(1, "Bebe");
        cout << "bebeRexha = " << storage.dump(bebeRexha) << endl;
        auto bebeRexhaMaybe = storage.get_no_throw<User>(1, "Bebe");
        auto insertedId = storage.insert(User{
            2,
            "Drake",
            "Singer",
        });
        storage.replace(User{
            2,
            "The Weeknd",
            "Singer",
        });
        auto weeknd = storage.get<User>(2, "The Weeknd");
        cout << "weeknd = " << storage.dump(weeknd) << endl;

fnc12 avatar Sep 21 '17 14:09 fnc12

@Ninja-007 is it ok for you? I don't know whether I need to add insert support which will insert only non-primary key columns. What do u think?

fnc12 avatar Sep 23 '17 12:09 fnc12

If I use a composite primary key, the table is always recreated while syncing the schema.

At line 6037 impl->schema_status always returns "sync_schema_result::dropped_and_recreated".

        template<class ...Tss, class ...Cs>
        sync_schema_result sync_table(storage_impl<table_t<Cs...>, Tss...> *impl, sqlite3 *db, bool preserve) {
            auto res = sync_schema_result::already_in_sync;
            
            auto schema_stat = impl->schema_status(db, preserve);
            if(schema_stat != decltype(schema_stat)::already_in_sync) {

boodkb avatar Oct 11 '17 09:10 boodkb

@boodkb please give me your code with make_storage call - I'll test and fix it

fnc12 avatar Oct 11 '17 12:10 fnc12


struct Record
{
    int year;
    int month;

    int amount;
};


    auto storage = make_storage("test.db",
                            make_table("records",
                                       make_column("year", &Record::year),
                                       make_column("month", &Record::month),
                                       make_column("amount", &Record::amount),

                                       primary_key(&Record::year, &Record::month)));

    storage.sync_schema();

boodkb avatar Oct 11 '17 13:10 boodkb

fixed bug with sync_schema and added to tests.cpp to prevent such a bug in future. I'm leaving the issue opened until I fix insert - make it ignore composite key columns

fnc12 avatar Oct 12 '17 17:10 fnc12

fixed. Now composite keys are totally supported by this lib. Specially thanks to @Ninja-007 and @boodkb .

fnc12 avatar Oct 12 '17 17:10 fnc12