Skip to content

Commit e45ceca

Browse files
authored
Merge pull request #2223 from etiennebr/force-field-type
Force field type when appending to unexisting table (Fix #2206)
2 parents 732525f + ac0a1dd commit e45ceca

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,5 +565,6 @@ importFrom(utils,packageVersion)
565565
importFrom(utils,str)
566566
importFrom(utils,tail)
567567
importMethodsFrom(DBI,dbDataType)
568+
importMethodsFrom(DBI,dbExistsTable)
568569
importMethodsFrom(DBI,dbWriteTable)
569570
useDynLib(sf, .registration=TRUE)

R/db.R

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -466,22 +466,27 @@ setMethod("dbWriteTable", c("PostgreSQLConnection", "character", "sf"),
466466
#' to PostgreSQL. See `dbDataType()` for details.
467467
#' @md
468468
#' @rdname dbWriteTable
469-
#' @importMethodsFrom DBI dbWriteTable
469+
#' @importMethodsFrom DBI dbWriteTable dbExistsTable
470470
#' @export
471471
setMethod("dbWriteTable", c("DBIObject", "character", "sf"),
472-
function(conn, name, value, ..., row.names = FALSE, overwrite = FALSE,
473-
append = FALSE, field.types = NULL, binary = TRUE) {
474-
if (is.null(field.types)) field.types <- dbDataType(conn, value)
475-
# DBI cannot set field types with append
476-
if (append) field.types <- NULL
477-
#tryCatch({
478-
dbWriteTable(conn, name, to_postgis(conn, value, binary),..., row.names = row.names,
479-
overwrite = overwrite, append = append,
480-
field.types = field.types)
481-
# }, warning=function(w) {
482-
# stop(conditionMessage(w), call. = FALSE) # nocov
483-
# })
484-
}
472+
function(conn, name, value, ..., row.names = FALSE, overwrite = FALSE,
473+
append = FALSE, field.types = NULL, binary = TRUE) {
474+
if (is.null(field.types)) field.types <- dbDataType(conn, value)
475+
476+
# DBI cannot set field types with append, but if the table does not exist,
477+
# we need to set the field type.
478+
if (append) {
479+
if (!dbExistsTable(conn, name)) {
480+
append <- FALSE
481+
} else {
482+
field.types <- NULL
483+
}
484+
}
485+
486+
dbWriteTable(conn, name, to_postgis(conn, value, binary),..., row.names = row.names,
487+
overwrite = overwrite, append = append,
488+
field.types = field.types)
489+
}
485490
)
486491

487492
to_postgis <- function(conn, x, binary) {

man/dbWriteTable.Rd

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_postgis_RPostgres.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ test_that("can write to db", {
6969
expect_silent(write_sf(pts, pg, "sf_meuse__", delete_layer = TRUE))
7070
})
7171

72+
test_that("can create a missing table even if append is TRUE (#2206)", {
73+
skip_if_not(can_con(pg), "could not connect to postgis database")
74+
x <- st_sf(geometry = st_sfc(st_point(1:2)))
75+
dbWriteTable(pg, "x", x, append = TRUE, temporary = TRUE)
76+
col_type <- dbGetQuery(pg, "SELECT pg_typeof(geometry) as col_type FROM x")
77+
expect_equal(unclass(col_type[["col_type"]]), "geometry")
78+
dbExecute(pg, "drop table if exists x")
79+
})
80+
7281
test_that("can handle multiple geom columns", {
7382
skip_if_not(can_con(pg), "could not connect to postgis database")
7483
multi <- cbind(pts[["geometry"]], st_transform(pts, 4326))

0 commit comments

Comments
 (0)