nestedFieldName :: forall record typ. PersistEntity record => NestedField record typ -> Text
nestedFieldName = T.intercalate "." . nesFldName
  where
    nesFldName :: forall r1 r2. (PersistEntity r1) => NestedField r1 r2 -> [DB.Label]
    nesFldName (nf1 `LastEmbFld` nf2)          = [fieldName nf1, fieldName nf2]
    nesFldName ( f1 `MidEmbFld`  f2)           = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFlds` f2)          = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFldsNullable` f2)  = fieldName f1 : nesFldName f2
    nesFldName (nf1 `LastNestFld` nf2)         = [fieldName nf1, fieldName nf2]
    nesFldName (nf1 `LastNestFldNullable` nf2) = [fieldName nf1, fieldName nf2]
nestedFieldName :: forall record typ. PersistEntity record => NestedField record typ -> Text
nestedFieldName = T.intercalate "." . nesFldName
  where
    nesFldName :: forall r1 r2. (PersistEntity r1) => NestedField r1 r2 -> [DB.Label]
    nesFldName (nf1 `LastEmbFld` nf2)          = [fieldName nf1, fieldName nf2]
    nesFldName ( f1 `MidEmbFld`  f2)           = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFlds` f2)          = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFldsNullable` f2)  = fieldName f1 : nesFldName f2
    nesFldName (nf1 `LastNestFld` nf2)         = [fieldName nf1, fieldName nf2]
    nesFldName (nf1 `LastNestFldNullable` nf2) = [fieldName nf1, fieldName nf2]
nestedFieldName = T.intercalate "." . nesFldName
  where
    nesFldName :: forall r1 r2. (PersistEntity r1) => NestedField r1 r2 -> [DB.Label]
    nesFldName (nf1 `LastEmbFld` nf2)          = [fieldName nf1, fieldName nf2]
    nesFldName ( f1 `MidEmbFld`  f2)           = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFlds` f2)          = fieldName f1 : nesFldName f2
    nesFldName ( f1 `MidNestFldsNullable` f2)  = fieldName f1 : nesFldName f2
    nesFldName (nf1 `LastNestFld` nf2)         = [fieldName nf1, fieldName nf2]
    nesFldName (nf1 `LastNestFldNullable` nf2) = [fieldName nf1, fieldName nf2]