2010/12/23 yufeng <[email protected]>: > hey: > I see Using a natural key (tutorial5.C),I define my XDBUser class, > #ifndef _XDBUSER_H > #define _XDBUSER_H > #include <Wt/Dbo/Dbo> > #include <Wt/Dbo/backend/Postgres> > #include <Wt/Dbo/backend/Sqlite3> > #include <string> > using namespace std; > class XDBUser { > public: > int mUid; > string mNo; > string mName; > string mKey; > template<class Action> > void persist(Action& a) > { > Wt::Dbo:: id(a, mUid, "fuid"); > Wt::Dbo::field(a, mNo, "fno"); > Wt::Dbo::field(a, mName, "fname"); > Wt::Dbo::field(a, mKey, "fkey"); > } > }; > #endif > > but error show in apache error_log file: > [2010-Dec-23 22:24:47.766448] 18171 [/AXXXWWeb.fcgi 62bl7qnCdbHkEphF] > [notice] "Session created (#sessions = 1)" > Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.2.13) Gecko/20101206 > Ubuntu/10.04 (lucid) Firefox/3.6.13 > [2010-Dec-23 22:24:47.815758] 18171 [/AXXXWWeb.fcgi 62bl7qnCdbHkEphF] > [fatal] "Error: Wt::Dbo::id() called for class C with surrogate key: > Wt::Dbo::dbo_traits<C>::surrogateIdField() != 0" > [2010-Dec-23 22:24:47.816613] 18171 [/AXXXWWeb.fcgi 62bl7qnCdbHkEphF] > [notice] "Session destroyed (#sessions = 0)" > if remove " Wt::Dbo:: id(a, mUid, "fuid");" ,there is no problem.
This is explained in detail the tutorial: http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html#_changing_or_disabling_the_surrogate_primary_key_id_field > anthor guestion,how to get id?if no Specifying a natural primary key,it > will auto add id field. when Querying objects,sometimes must get this > id ,how to get joe->id?like here: > (tutorial1.C continued) > dbo::ptr<User> joe = session.find<User>().where("name = ?").bind("Joe"); > > std::cerr << "Joe has karma: " << joe->karma << std::endl; > See reference documentation of Wt::Dbo::Dbo<>. If you want access to the id from within the object, you must inherit from Dbo::Dbo<>. If you have a dbo::ptr<> object and you want to know the ID, then you can use the id() method of dbo_ptr<>. So in your example: std::cerr << "Joe has id " << joe.id() << std::endl(); Best regards, Wim. ------------------------------------------------------------------------------ Learn how Oracle Real Application Clusters (RAC) One Node allows customers to consolidate database storage, standardize their database environment, and, should the need arise, upgrade to a full multi-node Oracle RAC database without downtime or disruption http://p.sf.net/sfu/oracle-sfdevnl _______________________________________________ witty-interest mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/witty-interest
