Trouble using a string as primary key in Wt::Dbo
Added by Matt Kingston over 12 years ago
Hi there,
I'm having trouble trying to use a string as my primary key in a database.
The errors I get are essentially telling me I must use a different type; but surely that's not the case? I suspect I need to overload one or more of these functions, but I'm not sure which or how, or whether that's even the correct course of action.
The errors I'm getting are as follows:
Error 1:
/usr/local/include/Wt/Dbo/DbAction_impl.h: In member function ‘void Wt::Dbo::LoadDbAction<C>::actId(V&, const string&, int) [with V = std::basic_string<char>, C = Business, std::string = std::basic_string<char>]’:
[... I've omitted some lines here for readability, full text in the link below ...]
/usr/local/include/Wt/Dbo/DbAction_impl.h:303:3: error: no matching function for call to ‘Wt::Dbo::MetaDbo<Business>::setId(std::basic_string<char>&)’
/usr/local/include/Wt/Dbo/DbAction_impl.h:303:3: note: candidate is:
/usr/local/include/Wt/Dbo/ptr:337:8: note: void Wt::Dbo::MetaDbo<C>::setId(const IdType&) [with C = Business, Wt::Dbo::MetaDbo<C>::IdType = long long int]
/usr/local/include/Wt/Dbo/ptr:337:8: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const IdType& {aka const long long int&}’
Error 2:
db test.cpp:12:5: instantiated from ‘void Business::persist(Action&) [with Action = Wt::Dbo::SaveDbAction<Business>]’
[... I've omitted some lines here for readability, full text in the link below ...]
/usr/local/include/Wt/Dbo/DbAction_impl.h:516:5: error: no matching function for call to ‘Wt::Dbo::MetaDbo<Business>::setId(std::basic_string<char>&)’
/usr/local/include/Wt/Dbo/DbAction_impl.h:516:5: note: candidate is:
/usr/local/include/Wt/Dbo/ptr:337:8: note: void Wt::Dbo::MetaDbo<C>::setId(const IdType&) [with C = Business, Wt::Dbo::MetaDbo<C>::IdType = long long int]
/usr/local/include/Wt/Dbo/ptr:337:8: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const IdType& {aka const long long int&}’
If required, the full compiler output is here: http://pastebin.com/5nRQXDXi
The code I'm using is essentially an extension of the code in the Wt::Dbo tutorial (http://goo.gl/QjUvV):
#include <Wt/WApplication>
#include <Wt/Dbo/Dbo>
#include <string>
class Business {
public:
std::string name;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::id(a, name, "name");
}
};
namespace Wt {
namespace Dbo {
template<>
struct dbo_traits<Business> : public dbo_default_traits {
typedef std::string IDType;
static IDType invalidID() {
return std::string();
}
static const char *surrogateIdField() { return 0; }
};
}
}
class DBTestApp : public Wt::WApplication {
public:
DBTestApp(const Wt::WEnvironment& env);
Wt::Dbo::Session *dbSession_;
};
DBTestApp::DBTestApp(const Wt::WEnvironment& env) : Wt::WApplication(env) {
typedef Wt::Dbo::collection< Wt::Dbo::ptr<Business> > Businesses;
dbSession_ = session;
Businesses businesses = dbSession_->find<Business>();
for (Businesses::const_iterator i = businesses.begin(); i != businesses.end(); ++i) {
business_name_->addItem((*i)->name);
}
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env) {
return new DBTestApp(env);
}
int main(int argc, char **argv) {
return Wt::WRun(argc, argv, &createApplication);
}
Thanks in advance for any help!
Matt
Replies (3)
RE: Trouble using a string as primary key in Wt::Dbo - Added by Matt Kingston over 12 years ago
I should mention that the compiler complains about this line:
business_name_addItem((*i)>name);
RE: Trouble using a string as primary key in Wt::Dbo - Added by Koen Deforche over 12 years ago
Hey,
The specialization of dbo_traits needs to be visible to the persist() implementation.
Thus:
class Business;
namespace Wt {
namespace Dbo {
template<>
struct dbo_traits<Business> : public dbo_default_traits {
typedef std::string IDType;
static IDType invalidID() {
return std::string();
}
static const char *surrogateIdField() { return 0; }
};
}
}
class Business {
public:
std::string name;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::id(a, name, "name");
}
};
Regards,
koen
RE: Trouble using a string as primary key in Wt::Dbo - Added by Matt Kingston over 12 years ago
Hi Koen,
Of course :(. As you may be able to tell given the answer to this question, I haven't coded C much for a few years and am still making some rookie errors. I really appreciate your help.
One day (hopefully not too far in the future) I'll be more proficient; at which time I'll make a point of giving back a bit :)
Thank you,
Matt