@@ -437,7 +437,7 @@ createBackend logFunc serverVersion smap conn =
437437 , connCommit = const $ PG. commit conn
438438 , connRollback = const $ PG. rollback conn
439439 , connEscapeFieldName = escapeF
440- , connEscapeTableName = escapeE . getEntityDBName
440+ , connEscapeTableName = \ entDef -> prependSchemaAndEscape entDef $ getEntityDBName entDef
441441 , connEscapeRawName = escape
442442 , connNoLimit = " LIMIT ALL"
443443 , connRDBMS = " postgresql"
@@ -760,7 +760,8 @@ getColumns :: (Text -> IO Statement)
760760 -> EntityDef -> [Column ]
761761 -> IO [Either Text (Either Column (ConstraintNameDB , [FieldNameDB ]))]
762762getColumns getter def cols = do
763- let sqlv = T. concat
763+ let tableSchema = fromMaybe " current_schema()" (getEntityDBSchema def)
764+ sqlv = T. concat
764765 [ " SELECT "
765766 , " column_name "
766767 , " ,is_nullable "
@@ -772,7 +773,7 @@ getColumns getter def cols = do
772773 , " ,character_maximum_length "
773774 , " FROM information_schema.columns "
774775 , " WHERE table_catalog=current_database() "
775- , " AND table_schema=current_schema() "
776+ , " AND table_schema=" <> tableSchema <> " "
776777 , " AND table_name=? "
777778 ]
778779
@@ -797,7 +798,7 @@ getColumns getter def cols = do
797798 , " information_schema.table_constraints AS k "
798799 , " WHERE c.table_catalog=current_database() "
799800 , " AND c.table_catalog=k.table_catalog "
800- , " AND c.table_schema=current_schema() "
801+ , " AND c.table_schema=" <> tableSchema <> " "
801802 , " AND c.table_schema=k.table_schema "
802803 , " AND c.table_name=? "
803804 , " AND c.table_name=k.table_name "
@@ -1338,6 +1339,12 @@ showAlter table (DropReference cname) = T.concat
13381339 , escapeC cname
13391340 ]
13401341
1342+ prependSchemaAndEscape :: EntityDef -> EntityNameDB -> Text
1343+ prependSchemaAndEscape entDef entDBName = case getEntityDBSchema entDef of
1344+ Nothing -> escapeE entDBName
1345+ Just " public" -> escapeE entDBName
1346+ Just schema -> escape schema <> " ." <> escapeE entDBName
1347+
13411348-- | Get the SQL string for the table that a PeristEntity represents.
13421349-- Useful for raw SQL queries.
13431350tableName :: (PersistEntity record ) => record -> Text
@@ -1574,7 +1581,7 @@ mockMigration mig = do
15741581 , connCommit = undefined
15751582 , connRollback = undefined
15761583 , connEscapeFieldName = escapeF
1577- , connEscapeTableName = escapeE . getEntityDBName
1584+ , connEscapeTableName = \ entDef -> prependSchemaAndEscape entDef $ getEntityDBName entDef
15781585 , connEscapeRawName = escape
15791586 , connNoLimit = undefined
15801587 , connRDBMS = undefined
0 commit comments