Wt::Dbo composite natural primary key issues
Added by Dawid Oriański over 7 years ago
Hello all!
I am currently attempting to create dbo with composite natural primary key, following tutorial.
I had a common pattern, when Child objects belong to a common Root object, with children having a unique name. So I created a template class BelongingName which functions as an id for each Child. It consists of WString name and Wt::Dbo::ptr foreign key.
With gcc 5.4.0 I get errors with this solution though, which can be divided in 2 categories:
First, conversion error
include/Wt/Dbo/Field_impl.h:53:28: error: no matching function for call to ‘Wt::Dbo::sql_value_traits::bind(BelongingName&, Wt::Dbo::SqlStatement*&, int&, const int&)’
sql_value_traits::bind(value_, statement, column, size_);
^with note about no known conversion of BelongingName into const char*. I don't mind implementing conversion operator, but I think that's what operator<< specialization is for:
namespace std{
ostream& operator<< (ostream& o, const BelongingName& bn)
{
return o << bn.getName() << '@' << bn.getRoot()->getName();
}
} // namespace std
<!-- -->
Second, sql_value_traits error:
include/Wt/Dbo/Field_impl.h:41:35: error: ‘type’ is not a member of ‘Wt::Dbo::sql_value_traits’
return sql_value_traits::type(session.connection(false), size_);
^It would seem, that sql_value_traits specialization is required. I was sure though, that BelongingName isn't an actual value, but merely an aggregation, as suggested by:
namespace Wt{
namespace Dbo{template<class Action> void field(Action& action, BelongingName& prop, const Wt::WString& name, int size = -1) { belongsTo(action, prop.getRoot(), name, NotNull | OnUpdateCascade | OnDeleteCascade); field(action, prop.getName(), name + "_name"); } } // namespace Dbo } // namespace Wt
I'm not sure if this is a bug report, a feature request, both, or just me being stupid :P Could someone clarify?
I'm including an example for reproduction.
example.cpp (2.81 KB) example.cpp |
Replies (2)
RE: Wt::Dbo composite natural primary key issues - Added by Koen Deforche over 7 years ago
Hey,
In principle this could work, but you are declaring/defining the field(Action& action, BelongingName& prop, const Wt::WString& name, int size = --1) override after it is being needed, and this leads to both of the compile issues you are mentioning.
Koen
RE: Wt::Dbo composite natural primary key issues - Added by Dawid Oriański over 7 years ago
Thanks for the good point Koen!
Indeed I should've moved field() override higher, but there was one more issue - I mistakenly changed the 'name' type to const Wt::WString& from const std::string& as it should be.
All in all I'm very happy with Wt, but since it already introduces WString type I wish it would use it everywhere, especially in Wt::Json ;)
Thanks and keep up the good work!