Skip to content
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

Issues with constraints #467

Closed
ab-pm opened this issue Aug 9, 2019 · 1 comment
Closed

Issues with constraints #467

ab-pm opened this issue Aug 9, 2019 · 1 comment

Comments

@ab-pm
Copy link
Contributor

ab-pm commented Aug 9, 2019

I noticed a few issues with setting constraints:

  • the references field for foreign keys is not escaped, leading to invalid syntax when using identifiers that are also keywords. I understand that this is because one might not only have the table name in there but also the column names (e.g. references: '"user"(id)'), but since using only the table name works in Postgres (defaulting to the primary key column) it would be really nice to escape this when the property holds a string that does not contain parenthesis. Maybe even split up in referencesTable: Name and referencesColumns: Name | Array<Name>? Please adjust the documentation that references currently expects a String, not a single (table) name.
  • referencesConstraintName totally makes sense in column definitions. However, it is also documented as an option for addConstraint, and using it there (together with the normal constraint_name - even when that is empty) totally breaks the generated SQL command.
  • I am not sure why the foreignKeys option for addConstraint is documented as an "[object or array of objects]". When I tried passing an array, it did create two CONSTRAINT statements with the same name which was of course rejected by Postgres.
  • Constraints don't support comments as an option, I have to make do with raw pgm.sql :-(
    I'd love if addConstraint could support a comment option, and if createTable/addColumns could support a referencesConstraintComment option.

I'll try to make a PR if you agree!

@dolezel
Copy link
Contributor

dolezel commented Aug 13, 2019

  • Yes references didn't escape as there can be also columns. But looking at documentation, it should be possible to check for parenthesis and escape expression if they are not present
  • There is a common function to parse reference definition, but it is true, that referencesConstraintName does not make sense in addConstraint.
  • Constraint name is derived from column names of foreign key, so you can pass array of foreign keys, but they should have unique set of columns (it does not even make sense otherwise)
  • Makes sense to add comment as an option when creating constraints

PRs welcome!

ab-pm added a commit to ab-pm/node-pg-migrate that referenced this issue Aug 14, 2019
* added escaping to `references` field when only a table name is given
* now also creates default constraint names when you don't pass anything in `addConstraint`

fixes salsita#467
ab-pm added a commit to ab-pm/node-pg-migrate that referenced this issue Aug 14, 2019
- add some tests (not exhaustive though)
- update docs
- a `referencesConstraintComment` on a column definition auto-generates a constraint name if none is given

Closes salsita#467.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants