Project

General

Profile

Wt::Dbo - Optinoal Many-to-one Relationship

Added by Aaron Wright about 1 year ago

I have a database object with an optional many-to-one relationship with another database object. When I fail to provide an object on the one-side of the relationship, I get an error from Wt:

Wt: error during event handling: Wt::Dbo::ptr<Bar>: null dereference
Wt: fatal error: Wt::Dbo::ptr<Bar>: null dereference

My one-side database object looks like this:

class Foo final : public Wt::Dbo::Dbo<Foo> {
public:

  template<typename TAction>
  void persist(TAction& action)
  {
    Wt::Dbo::belongsTo(action, bar_, "Bar");
  }

private:

  Wt::Dbo::ptr<Bar> bar_;
};

And the many-side database object looks like this:

class Bar final : public Wt::Dbo::Dbo<Bar> {
public:

  template<typename TAction>
  void persist(TAction& action)
  {
    Wt::Dbo::hasMany(
      action, foos_, Wt::Dbo::ManyToOne, "Bar");
  }

private:

  Wt::Dbo::collection<Wt::Dbo::ptr<Foo>> foos_;
};

How can I get this to work if I want to make bar_ optional in the one-side Foo objects?

The schema shows that the Bar_id column in the Foo table is nullable, but there is a foreign key constraint on that column. Is that stopping this?

Is there another way to accomplish this?


Replies (2)

RE: Wt::Dbo - Optinoal Many-to-one Relationship - Added by Roel Standaert about 1 year ago

The ptr is nullable by default. If Wt::Dbo dereferences it that would be a bug, but I don't think it normally does. The Wt::Dbo tests leave the ptr set to null rather often.

You'd have to figure out where you're dereferencing bar_. You can compile Wt with unwind to get backtraces, or configure your debugger to break when Wt::Dbo::Exception is thrown. You could also break on the specific places where the exception is thrown in Wt/Dbo/ptr_impl.h (on the latest master, those are lines 439, 459, and 468).

RE: Wt::Dbo - Optinoal Many-to-one Relationship - Added by Aaron Wright about 1 year ago

Well, that's embarrassing. You were right, I was dereferencing bar_ myself. Thanks for the tips though. They helped me figure out where I was going wrong.

    (1-2/2)