Skip to content

Commit

Permalink
fix: column constraing in pg
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhi8833998 committed Aug 2, 2024
1 parent d4a9577 commit bc4da90
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,10 @@ create_column_definition
}

column_constraint
= n:(literal_not_null / literal_null) __ df:default_expr? {
= n:constraint_name {
return { constraint: n }
}
/ n:(literal_not_null / literal_null) __ df:default_expr? {
// => { nullable: literal_null | literal_not_null; default_val: default_expr; }
if (n && !n.value) n.value = 'null'
return {
Expand Down
3 changes: 2 additions & 1 deletion src/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function generatedExpressionToSQL(generated) {
function columnOption(definition) {
const columnOpt = []
const {
nullable, character_set: characterSet, check, comment, collate, storage, using,
nullable, character_set: characterSet, check, comment, constraint, collate, storage, using,
default_val: defaultOpt, generated,
auto_increment: autoIncrement,
unique: uniqueKey,
Expand All @@ -117,6 +117,7 @@ function columnOption(definition) {
columnOpt.push(type.toUpperCase(), exprToSQL(value))
}
const { database } = getParserOpt()
if (constraint) columnOpt.push(toUpper(constraint.keyword), literalToSQL(constraint.constraint))
columnOpt.push(constraintDefinitionToSQL(check))
columnOpt.push(generatedExpressionToSQL(generated))
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment))
Expand Down
6 changes: 3 additions & 3 deletions test/postgres.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1589,10 +1589,10 @@ describe('Postgres', () => {
]
},
{
title: '',
title: 'create table with column_constraint',
sql: [
"CREATE TABLE public.tnotok (description text DEFAULT ''::text NOT NULL);",
`CREATE TABLE "public"."tnotok" (description TEXT NOT NULL DEFAULT ''::TEXT)`,
`CREATE TABLE public.tnotok ("id" SERIAL CONSTRAINT users_PK PRIMARY KEY, description text DEFAULT ''::text NOT NULL);`,
`CREATE TABLE "public"."tnotok" ("id" SERIAL CONSTRAINT users_PK PRIMARY KEY, description TEXT NOT NULL DEFAULT ''::TEXT)`,
]
},
]
Expand Down

0 comments on commit bc4da90

Please sign in to comment.