Skip to content

Commit 0da82b0

Browse files
authored
Merge pull request #1357 from uuiid/dev
2 parents cf82e1e + 886beac commit 0da82b0

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

dev/implementations/table_definitions.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ namespace sqlite_orm {
2525
if(auto d = column.default_value()) {
2626
dft = std::move(*d);
2727
}
28+
using constraints_tuple = decltype(column.constraints);
29+
constexpr bool hasExplicitNull =
30+
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>>, constraints_tuple>::value;
31+
constexpr bool hasExplicitNotNull =
32+
mpl::invoke_t<mpl::disjunction<check_if_has_type<not_null_t>>, constraints_tuple>::value;
2833
res.emplace_back(-1,
2934
column.name,
3035
type_printer<field_type>().print(),
31-
column.is_not_null(),
36+
!hasExplicitNull && !hasExplicitNotNull
37+
? column.is_not_null()
38+
: (hasExplicitNull ? !hasExplicitNull : hasExplicitNotNull),
3239
std::move(dft),
3340
column.template is<is_primary_key>(),
3441
column.template is<is_generated_always>());

include/sqlite_orm/sqlite_orm.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23838,10 +23838,17 @@ namespace sqlite_orm {
2383823838
if(auto d = column.default_value()) {
2383923839
dft = std::move(*d);
2384023840
}
23841+
using constraints_tuple = decltype(column.constraints);
23842+
constexpr bool hasExplicitNull =
23843+
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>>, constraints_tuple>::value;
23844+
constexpr bool hasExplicitNotNull =
23845+
mpl::invoke_t<mpl::disjunction<check_if_has_type<not_null_t>>, constraints_tuple>::value;
2384123846
res.emplace_back(-1,
2384223847
column.name,
2384323848
type_printer<field_type>().print(),
23844-
column.is_not_null(),
23849+
!hasExplicitNull && !hasExplicitNotNull
23850+
? column.is_not_null()
23851+
: (hasExplicitNull ? !hasExplicitNull : hasExplicitNotNull),
2384523852
std::move(dft),
2384623853
column.template is<is_primary_key>(),
2384723854
column.template is<is_generated_always>());

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ add_executable(unit_tests
144144
unique_cases/issue86.cpp
145145
unique_cases/issue937.cpp
146146
unique_cases/issue663.cpp
147+
unique_cases/issue1357.cpp
147148
get_all_custom_containers.cpp
148149
select_constraints_tests.cpp
149150
backup_tests.cpp

tests/unique_cases/issue1357.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <sqlite_orm/sqlite_orm.h>
2+
#include <catch2/catch_all.hpp>
3+
4+
#if SQLITE_VERSION_NUMBER >= 3006019
5+
using namespace sqlite_orm;
6+
7+
#ifdef SQLITE_ORM_OPTIONAL_SUPPORTED
8+
9+
TEST_CASE("issue1357") {
10+
struct Employee {
11+
int m_empno;
12+
std::string m_ename;
13+
std::string m_job;
14+
std::optional<int> m_mgr;
15+
std::string m_hiredate;
16+
double m_salary;
17+
std::optional<double> m_commission;
18+
int m_depno;
19+
};
20+
21+
struct Department {
22+
int m_deptno;
23+
std::string m_deptname;
24+
std::string m_loc;
25+
};
26+
27+
using namespace sqlite_orm;
28+
29+
auto storage = make_storage("",
30+
make_table("Emp",
31+
make_column("empno", &Employee::m_empno, primary_key().autoincrement()),
32+
make_column("ename", &Employee::m_ename),
33+
make_column("job", &Employee::m_job),
34+
make_column("mgr", &Employee::m_mgr),
35+
make_column("hiredate", &Employee::m_hiredate),
36+
make_column("salary", &Employee::m_salary),
37+
make_column("comm", &Employee::m_commission),
38+
make_column("depno", &Employee::m_depno),
39+
foreign_key(&Employee::m_depno).references(&Department::m_deptno)),
40+
make_table("Dept",
41+
make_column("deptno", &Department::m_deptno, primary_key().autoincrement()),
42+
make_column("deptname", &Department::m_deptname),
43+
make_column("loc", &Department::m_loc, null())));
44+
storage.sync_schema(true);
45+
storage.insert(Department{10, "Accounts", "New York"});
46+
storage.insert(Employee{1, "Paul", "Salesman", 2, "2002-02-12", 20000.0, 0.0, 1});
47+
storage.insert(Employee{2, "Allen", "Salesman", 2, "2002-02-12", 20000.0, 0.0, 1});
48+
REQUIRE_NOTHROW(storage.sync_schema(true));
49+
}
50+
#endif
51+
#endif

0 commit comments

Comments
 (0)