-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for primary keys in dbCreateTable() and sqlCreateTable() #351
base: main
Are you sure you want to change the base?
Conversation
field_names <- dbQuoteIdentifier(con, names(fields)) | ||
field_types <- unname(fields) | ||
fields <- paste0(field_names, " ", field_types) | ||
|
||
SQL(paste0( | ||
"CREATE ", if (temporary) "TEMPORARY ", "TABLE ", table, " (\n", | ||
" ", paste(fields, collapse = ",\n "), "\n)\n" | ||
" ", paste0(fields, nullable, collapse = ",\n "), | ||
if (!is.null(pk)) paste0(",\n PRIMARY KEY (", paste(dbQuoteIdentifier(con, pk), collapse = ", "), ")"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How standard is this syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty much, I think, but I don't know. We'll find out soon enough, we can override in backends -- this is a method.
R/table-create.R
Outdated
@@ -24,6 +24,10 @@ NULL | |||
#' @param temporary If `TRUE`, will generate a temporary table statement. | |||
#' @inheritParams rownames | |||
#' @param ... Other arguments used by individual methods. | |||
#' @param pk Primary key columns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems too short compared to other argument names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
primary.key
? I'm not sure this package already uses underscores for argument names :-\
Co-authored-by: Hadley Wickham <[email protected]>
As I understand it, DBI tries to be SQL 99 compliant. If so, I believe the standard syntax is in fact CREATE TABLE {tableName} (
{column Definitions},
CONSTRAINT {PK_Name} PRIMARY KEY ({column Names})
) Per W3Schools, the |
I was looking for a way to add a primary key when using
|
@vnijs: The dm package supports this via |
@krlmlr
|
This is important, because it is difficult and computationally intensive (and sometimes impossible) to change the primary key after creating the table.
Should we also support foreign keys and unique constraints?
This is a blocker for cynkra/dm#3. We would also need to adapt
dbplyr::copy_to()
and perhapsdbplyr::compute()
.