Project

General

Profile

Support for composite primary keys in dbo

Added by Michael Wagner over 13 years ago

Hi wt-Team,

Since composite primary keys are supported by wt now I tried to make use of this feature in my implementation but I got stuck. It would be great if you could provide a code example for one class / table that has a composite primary key (e.g. consisting of two columns of type integer). I attached my code with the "CompositeKey" class and the class that would use the composite key as well as the error output of the compiler.

As always, many, many thanks for your support,

Michael

CompositeKey.h (472 Bytes) CompositeKey.h The class defining the composite primary key
Presentation.h (1.27 KB) Presentation.h Some base class
RasterPresentation.h (1.58 KB) RasterPresentation.h The class inheriting from "Presentation" and causing the errors
error.txt (12.5 KB) error.txt The error output of the compiler

Replies (3)

RE: Support for composite primary keys in dbo - Added by Koen Deforche over 13 years ago

Hey Michael,

There were two problems.

The first was a problem in Wt::Dbo when loading an object using a natural id. Oops! This slipped through our automated tests of Wt::Dbo, and so we have also added more tests.

This has been fixed now in git.

The patch itself is rather small, so I have also included it here in case you would rather not switch to the git version of Wt.

The second is a few misconceptions related to the usage of composite keys.

This is a proper class definition for a composite key class:

namespace Domain
{
  class CompositeKey
  {
  public:
    int id_;
    short idMandator_;

    CompositeKey();
    CompositeKey(int id, short idMandator);
    CompositeKey (const CompositeKey &src);
    virtual ~CompositeKey();
    bool operator== (const CompositeKey& other) const;
    bool operator< (const CompositeKey& other) const;
  };

  extern std::ostream& operator<< (std::ostream&, const CompositeKey& key);
}

namespace Wt {
  namespace Dbo {

    template <class Action>
      void field(Action& action, Domain::CompositeKey& compositeKey, const std::string& name, int size = -1)
      {
        field(action, compositeKey.id_, name);
        field(action, compositeKey.idMandator_, name + "Mandator");
      }
  }
}

So the field() declaration simply needs to be in global scope, not inside your class. Otherwise your code looked good !

Regards

koen

composite-key-load.patch (4.68 KB) composite-key-load.patch Patch for loading with composite keys

RE: Support for composite primary keys in dbo - Added by Michael Wagner over 13 years ago

Hi Koen,

Thanks a lot for your prompt reply! Applying your changes it looks much better now, there is only a linker error left related to the streaming operator.

Would have any idea on this one?

Building target: I3GIS.wt

Invoking: GCC C Linker

g -L/usr/local/lib -o"I3GIS.wt" ./src/ui/MapDescriptionDialog.o ./src/ui/MapDialog.o ./src/ui/RemoveLayerDialog.o ./src/ui/RenameLayerDialog.o ./src/persistence/DaoFactory.o ./src/persistence/FeatureLayerDao.o ./src/persistence/FeaturePresentationDao.o ./src/persistence/MapDao.o ./src/persistence/RasterLayerDao.o ./src/persistence/RasterPresentationDao.o ./src/exceptions/Exception.o ./src/domain/CompositeKey.o ./src/domain/FeatureLayer.o ./src/domain/FeaturePresentation.o ./src/domain/Layer.o ./src/domain/Map.o ./src/domain/Presentation.o ./src/domain/RasterLayer.o ./src/domain/RasterPresentation.o ./src/I3GIS.o ./src/I3GISApp.o -lwthttp -lwtdbopostgres -lboost_signals -lwtext -lwt

./src/domain/RasterPresentation.o: In function `boost::detail::lexical_stream<std::basic_string<char, std::char_traits, std::allocator >, Domain::CompositeKey, std::char_traits >::operator<<(Domain::CompositeKey const&)':

RasterPresentation.cpp:(.text.ZN5boost6detail14lexical_streamISsN6Domain12CompositeKeyESt11char_traitsIcEElsERKS3[boost::detail::lexical_stream<std::basic_string<char, std::char_traits, std::allocator >, Domain::CompositeKey, std::char_traits >::operator<<(Domain::CompositeKey const&)]+0x1d): undefined reference to `Domain::operator<<(std::basic_ostream<char, std::char_traits >&, Domain::CompositeKey const&)'

collect2: ld returned 1 exit status

make: * [I3GIS.wt] Error 1

RE: Support for composite primary keys in dbo - Added by Michael Wagner over 13 years ago

Hi Koen,

Problem fixed. I had forgotten the actual definition of the operator<<, so far there was only its declaration.

Many thanks,

Michael

    (1-3/3)