diff --git a/pg-schema.cabal b/pg-schema.cabal
--- a/pg-schema.cabal
+++ b/pg-schema.cabal
@@ -1,6 +1,6 @@
 cabal-version:  3.12
 name:           pg-schema
-version:        0.5.0.0
+version:        0.5.1.0
 category:       Database
 author:         Dmitry Olshansky
 maintainer:     olshanskydr@gmail.com
diff --git a/src/PgSchema/Schema.hs b/src/PgSchema/Schema.hs
--- a/src/PgSchema/Schema.hs
+++ b/src/PgSchema/Schema.hs
@@ -114,27 +114,27 @@
     GetRelTab '[] ('(c,d) ':ys) s =
       If (NnsName c == s) '(RdFrom d, RelMany) (GetRelTab '[] ys s)
 
-type family Elem' (x :: Symbol) (xs :: [Symbol]) :: Bool where
-  Elem' x '[] = False
-  Elem' x (x ': xs) = True
-  Elem' x (y ': xs) = Elem' x xs
-
 type IsMandatory fd = Not (FdNullable fd || FdHasDefault fd)
 type IsMandatory' sch tab fld = IsMandatory (GetFldDef sch tab fld)
 
 type family RestMandatory' sch t (rs :: [Symbol]) (fs :: [Symbol]) (res :: [Symbol]) :: [Symbol] where
   RestMandatory' sch t rs '[] res = res
-  RestMandatory' sch t rs (fld ': fs) res = RestMandatory' sch t rs fs
-    (If (IsMandatory' sch t fld && Not (Elem' fld rs)) (fld ': res) res)
+  RestMandatory' sch t rs (fld ': fs) res =
+    RestMandatory'' sch t rs fs res fld (IsMandatory' sch t fld && Not (Elem' fld rs))
 
+type family RestMandatory'' (sch :: k) (t :: NameNSK) (rs :: [Symbol])
+  (fs :: [Symbol]) (res :: [Symbol]) (fld :: Symbol) (b :: Bool) :: [Symbol] where
+    RestMandatory'' sch t rs fs res fld 'True = fld ': RestMandatory' sch t rs fs res
+    RestMandatory'' sch t rs fs res fld 'False = RestMandatory' sch t rs fs res
+
 type RestMandatory sch t rs = RestMandatory' sch t rs (TdFlds (TTabDef sch t)) '[]
 
-type family RestPK' sch t (rs :: [Symbol]) (fs :: [Symbol]) (res :: [Symbol]) :: [Symbol] where
-  RestPK' sch t rs '[] res = res
-  RestPK' sch t rs (fld ': fs) res =
-    RestPK' sch t rs fs (If (Not (Elem' fld rs)) (fld ': res) res)
+type family RestPK' (rs :: [Symbol]) (fs :: [Symbol]) (res :: [Symbol]) :: [Symbol] where
+  RestPK' rs '[] res = res
+  RestPK' rs (fld ': fs) res =
+    RestPK' rs fs (If (Not (Elem' fld rs)) (fld ': res) res)
 
-type RestPK sch t rs = RestPK' sch t rs (TdKey (TTabDef sch t)) '[]
+type RestPK sch t rs = RestPK' rs (TdKey (TTabDef sch t)) '[]
 
 simpleType :: Text -> TypDef
 simpleType c = TypDef c Nothing []
diff --git a/src/PgSchema/Types.hs b/src/PgSchema/Types.hs
--- a/src/PgSchema/Types.hs
+++ b/src/PgSchema/Types.hs
@@ -45,7 +45,7 @@
 import Data.Type.Bool (Not, type (&&), type (||))
 import PgSchema.Schema
 import PgSchema.Utils.Internal hiding (fromText)
-
+import PgSchema.Utils.TF
 
 #ifdef MK_ARBITRARY
 import Test.QuickCheck(Arbitrary(arbitrary), arbitraryBoundedEnum)
diff --git a/src/PgSchema/Utils/TF.hs b/src/PgSchema/Utils/TF.hs
--- a/src/PgSchema/Utils/TF.hs
+++ b/src/PgSchema/Utils/TF.hs
@@ -60,3 +60,8 @@
 type family Map1 (f :: a ~> b) (xs :: [a]) :: [b] where
   Map1 f '[] = '[]
   Map1 f (x ': xs) = Apply f x ': Map1 f xs
+
+type family Elem' (x :: Symbol) (xs :: [Symbol]) :: Bool where
+  Elem' x '[] = False
+  Elem' x (x ': xs) = True
+  Elem' x (y ': xs) = Elem' x xs
