             sourceAttributeName = srcA.name
           }
           else {
-            fatalError("could not derive foreign key for: \(self.name)")
+            throw JoinCalculationError
+                    .toOneCouldNotDeriveForeignKey(entityName: self.name)
           }
         }
         else {
-          fatalError("could not derive foreign key for: \(self.name)")
+          throw JoinCalculationError
+                  .toOneCouldNotDeriveForeignKey(entityName: self.name)
         }
       }
     }

      // specify the attributes manually.
      if let n = self.targetAttributeName     { targetAttributeName = n    }
      else if let pkey = targetPrimaryKeyName { targetAttributeName = pkey }
      else                                    { return nil                 }
      
      if let n = self.sourceAttributeName     { sourceAttributeName = n    }
      else {

  }
  var targetPrimaryKeyName : String? {
    guard let pkeys = destinationEntity?.primaryKeyAttributeNames
     else { return nil }
    
    guard pkeys.count == 1 else { return nil }
    return pkeys[0]
  }
  
  
  // MARK: - Description

  override open func appendJoinsToDescripton(_ ms: inout String) {
    do {
      let join = try calculateJoin()
      if joinSemantic != .innerJoin { ms += " \(joinSemantic)" }
      ms += " "
      ms += join.shortDescription
    }
    catch {
      ms += " JOIN-ERROR<\(error)>"
    }
  }
}

open class CodeRelationship<Target: DatabaseObject>
             : CodeRelationshipBase<Target>, CodeRelationshipType
{
  public var codeEntity : Entity? = nil
  override open var entity : Entity {
    set {
      codeEntity = newValue
    }
    get {
      if let entity = codeEntity { return entity }
      fatalError("code entity has not been resolved!")
    }
  }

}


fileprivate let fakeEntity = ModelEntity(name: "FAKE")
  
