I have 2 objects, ObjectA and ObjectB.
When ObjectA gets created, ObjectB doesn’t (and can’t, due to not having the data yet) exist yet.
Once ObjectB is created, it needs to have a corresponding ObjectA attached to it if an appropriate one exists. If there isn’t an appropriate ObjectA, then the new ObjectB simply isn’t connected to one.
So, all ObjectA instances will eventually be attached to an ObjectB, but not all ObjectB instances will have an ObjectA.
Essentially, I’m looking for GORM to build database tables like this:
ObjectA
- Id (NotNull, unique)
- ObjectB_Id[FK: ObjectB.Id] (unique)
ObjectB
- Id (NotNull, unique)
How can I put together the GORM domain classes to do this?
I’ve tried just about every combination of hasOne, belongsTo, raw properties, nullable: true and unique: true constraints I can think of, but I must be missing one. This doesn’t seem like it’s a particularly odd scenario, so there must be some way to accomplish this.
As is often the case, putting my thoughts together into a question lead me to the solution:
The only catch with this that I can’t seem to get around is that it’s possible to set
ObjectB.objectAto anObjectAthat is already associated with anObjectB. When saving the new association, GORM just doesn’t save the association. No errors are thrown, and pulling the newObjectBout of the database leaves it’sobjectAproperty unset.Adding
unique: trueto theobjectAconstraints in theObjectBclass doesn’t help either. Instead of silently failing, an error is thrown indicating that the uniqueness check is failing due to an un-set parameter.