Ptr object generates 'stoll' on load
Added by Mark Petryk almost 3 years ago
I have a 'custom' table I have imported from an old Foxpro database in to Postgres. The Foxpro version does not have an ID column, per se. It has an "item_num" column, which is just text.
I have created a custom DBO class with custom ID field and version field.
35 template<>
36 struct dbo_traits< Exporter3pl::Product > : public dbo_default_traits
37 {
38 static const char * surrogateIdField()
39 {
40 return "item_num";
41 }
42
43 static const char * versionField()
44 {
45 return nullptr;
46 }
47
48 };
212 class Product
213 : public BaseClass< Product >
214 {
215 public:
216
217 template<class Action> void persist( Action &a )
218 {
219 Wt::Dbo::field( a, m_owner , "owner" );
220 Wt::Dbo::field( a, m_item_num , "item_num" );
221 Wt::Dbo::field( a, m_descriptio , "descriptio" );
222 Wt::Dbo::field( a, m_container , "container" );
223 Wt::Dbo::field( a, m_contperplt , "contperplt" );
224 Wt::Dbo::field( a, m_lotTracking , "lot_track" );
225 Wt::Dbo::field( a, m_grossWt , "gross_wt" );
226 Wt::Dbo::field( a, m_uniquecode , "uniquecode" );
227
228 } // endtemplate<class Action> void persist( Action &a )
229
230 std::string m_owner ;
231 std::string m_item_num ;
232 std::string m_descriptio ;
233 std::string m_container ;
234 int m_contperplt ;
235 int m_lotTracking ;
236 float m_grossWt ;
237 std::string m_uniquecode ;
238
239 }; // endclass Product
When I try to 'load' items from this table, it seems to load the items that begin with a 'numeric' value, but when an item_id begins with an alpha character, that seems to be when it generates the stoll error. I am assuming because the "id" field is expected to be numeric?
source file line row item_id
------------------------ --- --- -------------
/src/ExporterCamelot.cpp: 80 39 325101
/src/ExporterCamelot.cpp: 80 40 40844
/src/ExporterCamelot.cpp: 80 41 4601240209
/src/ExporterCamelot.cpp: 80 42 04-91
/src/ExporterCamelot.cpp: 80 43 5601230522
/src/ExporterCamelot.cpp: 80 44 597
/src/ExporterCamelot.cpp: 80 45 648 DAMAGED
/src/ExporterCamelot.cpp: 80 46 8-92D
[Switching to Thread 0x7fffdffff700 (LWP 3104)]
Thread 9 "warehouse" hit Catchpoint 1 (exception thrown), 0x00007ffff64418bd in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#0 0x00007ffff64418bd in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00007ffff646a20f in std::__throw_invalid_argument(char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff6c108ba in __gnu_cxx::__stoa<long long, long long, char, int> (__idx=0x0, __str=0x7fffdfff9870 "M5B4G61", __name=0x7ffff6c2043d "stoll", __convf=<optimized out>) at /usr/include/c++/5/ext/string_conversions.h:65
#
# Stoll error generated here, with the string of the next item in the sequence: "M5B4G61"
#
#3 std::__cxx11::stoll (__base=10, __idx=0x0, __str="M5B4G61") at /usr/include/c++/5/bits/basic_string.h:5275
#4 Wt::Dbo::backend::PostgresStatement::getResult (this=<optimized out>, column=<optimized out>, value=0x7fffdfff98f8) at /home/mark/projects/wt/wt4/src/Wt/Dbo/backend/Postgres.C:528
#5 0x00000000012a2eec in Wt::Dbo::Session::loadWithLongLongId<ExporterCamelot::Product> (this=0x7fffcc002d38, statement=0x7fffc00225c0, column=@0x7fffdfff9a3c: 1) at /opt/Wt4/include/Wt/Dbo/Session_impl.h:177
#6 0x000000000129fe1a in Wt::Dbo::Impl::LoadHelper<ExporterCamelot::Product, long long>::load (session=0x7fffcc002d38, statement=0x7fffc00225c0, column=@0x7fffdfff9a3c: 1) at /opt/Wt4/include/Wt/Dbo/Session_impl.h:34
#7 0x000000000129de45 in Wt::Dbo::Session::Mapping<ExporterCamelot::Product>::load (this=0x7fffcc01ecb0, session=..., statement=0x7fffc00225c0, column=@0x7fffdfff9a3c: 1) at /opt/Wt4/include/Wt/Dbo/Session_impl.h:432
So, it seems like the "ID Field" needs to be completely numeric? Is that true, an ID field cannot have any string-text in it?
Replies (4)
RE: Ptr object generates 'stoll' on load - Added by Korneel Dumon almost 3 years ago
Hi Mark,
I've never actually tried this myself, so I'm not sure, but from this part of the tutorial it would seem you can use a varchar
as a natural primary key. I think you're missing the typedef std::string IdType;
and should use Wt::Dbo::id()
instead of Wt::Dbo::field()
.
Another thing that came to mind was that you could add a numeric auto-incrementing id
column to the table before using it in Dbo.
RE: Ptr object generates 'stoll' on load - Added by Mark Petryk almost 3 years ago
Ah Ha! Tutorials!
Interesting, so I added those codes as per the tutorial. Now, it is not crashing, but it is also not returning any records, so I'll have to drill in and figure that out. Once I do I'll post a reply back here.
In the mean time, though, I did add an ID column just to get around the issue.
Thank you for the follow up.
RE: Ptr object generates 'stoll' on load - Added by Mark Petryk almost 3 years ago
I'm still tinkering, but I used the id() on the field, and got this;
"Wt: fatal error: Error: Wt::Dbo::id() called for class C with surrogate key: Wt::Dbo::dbo_traits::surrogateIdField() != 0"
35 template<>·
36 struct dbo_traits< ExporterCamelot::Product > : public dbo_default_traits·
37 {·
38 typedef std::string IdType;·
39 ·
40 static IdType invalidId()·
41 {·
42 return std::string();·
43 }·
44 ·
45 static const char * surrogateIdField()·
46 {·
47 return "item_num";·
48 }·
49 ·
50 static const char * versionField()·
51 {·
52 return nullptr;·
53 }·
54 ·
55 };·
219 class Product·
220 : public BaseClass< Product >·
221 {·
222 public:·
223 ·
224 template<class Action> void persist( Action &a )·
225 {·
226 Wt::Dbo::field( a, m_owner , "owner" );·
227 Wt::Dbo::id ( a, m_item_num , "item_num" );·
228 Wt::Dbo::field( a, m_descriptio , "descriptio" );·
229 Wt::Dbo::field( a, m_container , "container" );·
230 Wt::Dbo::field( a, m_contperplt , "contperplt" );·
231 Wt::Dbo::field( a, m_lotTracking , "lot_track" );·
232 Wt::Dbo::field( a, m_grossWt , "gross_wt" );·
233 Wt::Dbo::field( a, m_uniquecode , "uniquecode" );·
234 ·
235 } // endtemplate<class Action> void persist( Action &a )·
236 ·
237 std::string m_owner ;·
238 std::string m_item_num ;·
239 std::string m_descriptio ;·
240 std::string m_container ;·
241 std::string m_contperplt ;·
242 std::string m_lotTracking ;·
243 std::string m_grossWt ;·
244 std::string m_uniquecode ;·
245 ·
246 }; // endclass Product·
RE: Ptr object generates 'stoll' on load - Added by Mark Petryk almost 3 years ago
Tips for the impatient: read ALL the tutorial!
Thank you, Korneel. It works perfectly!
35 template<>·
36 struct dbo_traits< ExporterCamelot::Product > : public dbo_default_traits·
37 {·
38 typedef std::string IdType;·
39 ·
40 static IdType invalidId()·
41 {·
42 return std::string();·
43 }·
44 ·
45 static const char * surrogateIdField()·
46 {·
47 return 0; // this will be "item_num" in the Dbo class;·
48 }·
49 ·
50 static const char * versionField()·
51 {·
52 return nullptr;·
53 }·
54 ·
55 };·
219 class Product·
220 : public BaseClass< Product >·
221 {·
222 public:·
223 ·
224 template<class Action> void persist( Action &a )·
225 {·
226 Wt::Dbo::field( a, m_owner , "owner" );·
227 Wt::Dbo::id ( a, m_item_num , "item_num" );·
228 Wt::Dbo::field( a, m_descriptio , "descriptio" );·
229 Wt::Dbo::field( a, m_container , "container" );·
230 Wt::Dbo::field( a, m_contperplt , "contperplt" );·
231 Wt::Dbo::field( a, m_lotTracking , "lot_track" );·
232 Wt::Dbo::field( a, m_grossWt , "gross_wt" );·
233 Wt::Dbo::field( a, m_uniquecode , "uniquecode" );·
234 ·
235 } // endtemplate<class Action> void persist( Action &a )·
236 ·
237 std::string m_owner ;·
238 std::string m_item_num ;·
239 std::string m_descriptio ;·
240 std::string m_container ;·
241 std::string m_contperplt ;·
242 std::string m_lotTracking ;·
243 std::string m_grossWt ;·
244 std::string m_uniquecode ;·
245 ·
246 }; // endclass Product·