fnc12 / sqlite_orm

❤️ SQLite ORM light header only library for modern C++
GNU Affero General Public License v3.0
2.19k stars 305 forks source link

why sync_schema() allways drop records and recreate table? #1328

Closed newgolo closed 3 days ago

newgolo commented 3 days ago

when I run my sqlite_orm demo, I found that every time it will drop records and recreate a new table. I am so confused ..., code like following:

#include <sqlite_orm/sqlite_orm.h>
using namespace sqlite_orm; // NOLINT
struct Student {
    int id;
    std::string name;
};

inline auto initStorage(const std::string& path) {
    return make_storage(
        path,
        make_table(
            "table_students",
            make_column("id", &Student::id, primary_key().autoincrement()),
            make_column("name", &Student::name, default_value(""))));
}
using Storage = decltype(initStorage(""));

int main(int argc, char** argv) {
    auto storage = initStorage("test.db");
    storage.sync_schema();
    storage.insert<Student>({0, "Tom"});
    storage.insert<Student>({0, "Jimmy"});
    return 0;
}
fnc12 commented 3 days ago

please show me your original SQLs for all the tables

newgolo commented 3 days ago

SQLite version 3.22.0 2018-01-22 18:45:57 Enter ".help" for usage hints. sqlite> .schema CREATE TABLE IF NOT EXISTS "table_students" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "name" TEXT DEFAULT ('') NOT NULL ); CREATE TABLE sqlite_sequence(name,seq);

fnc12 commented 3 days ago

@newgolo and if you run sync_schema again you'll get table_students dropped and recreated?

newgolo commented 3 days ago

@newgolo and if you run sync_schema again you'll get table_students dropped and recreated?

Yes.

fnc12 commented 3 days ago

ok. Which lib version you use?

newgolo commented 3 days ago

ok. Which lib version you use?

sqlite: 3.22.0, sqlite_orm: 1.8.2.

fnc12 commented 3 days ago

please try dev branch instead - we have several fixes in dev, probably your bug was already fixed

newgolo commented 3 days ago

please try dev branch instead - we have several fixes in dev, probably your bug was already fixed

I tried the dev branch, but the problem is still unsolved.

newgolo commented 3 days ago

please try dev branch instead - we have several fixes in dev, probably your bug was already fixed

I find the problem, the sqlite3 version too old, upgrade to 3.31.1, it is ok! Thanks!