Added flag has_primary_key_set to Model
Now we know whether the primary key is defined or not and we do not allow to make update/remove if the key is not defined. And when doing insert/update we can put NULL if child models don't have the primary key set (fields with has_foreign_key set to true). Now in after_select() we should also set has_primary_key_set flag or just call get_last_sequence_for_primary_key instead of get_last_sequence. fixed: added prefix +00 when serializing PT::Date to PostgreSQL (time zone) (for a column with a time zone there was a wrong value saved)
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2019, Tomasz Sowa
|
||||
* Copyright (c) 2019-2021, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "morm.h"
|
||||
#include "language.h"
|
||||
#include "attachment.h"
|
||||
#include "attachment2.h"
|
||||
|
||||
|
||||
|
||||
@@ -64,29 +65,31 @@ class Person : public morm::Model
|
||||
public:
|
||||
|
||||
long id;
|
||||
|
||||
std::wstring first_name;
|
||||
std::wstring last_name;
|
||||
std::wstring email;
|
||||
Language language;
|
||||
|
||||
std::list<Attachment> attachments;
|
||||
Attachment attachment;
|
||||
Attachment2 attachment2;
|
||||
|
||||
|
||||
void map_fields()
|
||||
{
|
||||
field(L"id", id, false, false, true);
|
||||
//field(L"id", id, f::no_insertable | f::no_updatable | f::primary_key);
|
||||
field(L"language_id", L"language", language);
|
||||
|
||||
field(L"first_name", first_name);
|
||||
field(L"last_name", last_name);
|
||||
field(L"email", email);
|
||||
field(L"language_id", L"language", language);
|
||||
|
||||
field(L"person_id", L"attachments", attachments);
|
||||
|
||||
field(L"person_id", L"attachment", attachment, true, true, false);
|
||||
field(L"person_id", L"attachment2", attachment2, true, true, false);
|
||||
|
||||
|
||||
//field(L"id", id, f::no_insertable | f::no_updatable | f::primary_key);
|
||||
//field(L"person_id", attachment, f::insertable | f::updatable | f::foreign_key);
|
||||
//field(L"person_id", attachment, f::insertable, f::updatable, f::foreign_key);
|
||||
}
|
||||
@@ -99,13 +102,19 @@ public:
|
||||
|
||||
void after_select()
|
||||
{
|
||||
morm::Finder<Attachment> finder(model_connector);
|
||||
attachments = finder.select().where().eq(L"person_id", id).get_list();
|
||||
if( has_primary_key_set )
|
||||
{
|
||||
morm::Finder<Attachment> finder(model_connector);
|
||||
attachments = finder.select().where().eq(L"person_id", id).get_list();
|
||||
|
||||
morm::Finder<Attachment2> finder2(model_connector);
|
||||
attachment2 = finder2.select().where().eq(L"person_id", id).get();
|
||||
}
|
||||
}
|
||||
|
||||
void after_insert()
|
||||
{
|
||||
get_last_sequence(L"public.person_id_seq", id);
|
||||
get_last_sequence_for_primary_key(L"public.person_id_seq", id);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user