Feature #3400
openWt::Dbo::field() with prefix
0%
Description
In the test file, there is a case where there can't be more than 1 fields that represent a Dbo's ID if that Dbo is using a composite key. The test file include 2 Dbos:
- Module - Uses default surrogate key
- Page - Uses composite key of type 'long long' and 'Wt::Dbo::ptr'
Page Dbo also has a ManyToOne relation with Page Dbo itself, representing parent and children pages. Creating tables fails because, there is a duplicate Page_id and Module_id. I tried concatenating the field() arguments, however, it only causes relation with Module to break. I couldn't include the case with concatenated field()s, I will upload that too as soon as my computer is ready.
I suggest that to solve this problem, Wt::Dbo::field should have an extra argument for adding prefix to the field name.
Files
Updated by Koen Deforche about 10 years ago
- Status changed from New to InProgress
- Assignee set to Koen Deforche
- Target version set to 3.3.4
Updated by Koen Deforche about 10 years ago
- Status changed from InProgress to Feedback
Hey,
You're supposed to use the 'name' then to concatenate with the composite field id's.
See for example:
//Mapping for PageKeys composite key
namespace Wt
{
namespace Dbo
{
template<class Action>
void field(Action &action, PageKeys &Keys, const std::string &name, int size = -1)
{
field(action, Keys.id, name + "_page_id");
belongsTo(action, Keys.ModulePtr, name + "_Module", Wt::Dbo::OnDeleteCascade | Wt::Dbo::OnUpdateCascade | Wt::Dbo::NotNull);
}
}
}
Updated by Saif Rehman about 10 years ago
I've tried that. Did not work because the name in hasMany() and name in belongsTo() has to be the same. I've failed on this method and couldn't get it to work for quite a long time.
Updated by Saif Rehman about 10 years ago
Database error creating tables: Relation mismatch for table 'modules': no matching belongsTo() found in table 'pages' with name 'Module', but did find with name 'Page_Module'?
Throws once
Updated by Koen Deforche about 10 years ago
Hey,
I added this exact code as a test case (DboTestCompositeKey.C) in git, and it's working?
Koen
Updated by Saif Rehman about 10 years ago
Yea ... It throws only once for some reason... It is caught when an attempt to drop tables is made. If you try any other operation it throws weird stl library related exceptions.
Remove the lines starting from line 150
//Drop/Create
try
{
session_->dropTables(); //todo:remove
}
catch(...)
{ }
Updated by Saif Rehman almost 9 years ago
- File DboTestCompositeKey.C DboTestCompositeKey.C added
Here is the update version of the test case, the previous test case was suppressing the error somehow.