尝试创建类型时的Postgresql语法错误

时间:2019-01-02 06:23:18

标签: node.js postgresql

我正在尝试使用postgresql nodejs包在pg中创建以下类型。我编写了一个查询池并尝试如下创建此类型的函数:

return pool.query(
   `
        CREATE TYPE grade_sheet AS (
            subjectName VARCHAR(100),
            teacherName VARCHAR(100),
            uti VARCHAR(32),
            markAllocated REAL CHECK (markAllocated >= 0.0 AND markAllocated <= 100.00),
            markObtained REAL CHECK (markObtained >= 0.0 AND markObtained <= 100.00),
            gradeObtained CHAR(2),
            dateTaken TIMESTAMP
        );
   `
);

当我尝试运行脚本时,得到以下syntax error

{ error: syntax error at or near "CHECK"
at Connection.parseE (/home/zerocool/myschool/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/home/zerocool/myschool/node_modules/pg/lib/connection.js:379:19)
at Socket.<anonymous> (/home/zerocool/myschool/node_modules/pg/lib/connection.js:119:22)
at Socket.emit (events.js:127:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:590:20)
name: 'error',
length: 95,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '195',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1087',
routine: 'scanner_yyerror' }

1 个答案:

答案 0 :(得分:1)

约束不能在类型中使用。但是在领域中可以。但是,域不能具有多个属性。但是您可以同时使用以下方法解决问题:

  1. 创建一个包含您的检查约束的域
  2. 使用域创建类型

它看起来像:

CREATE DOMAIN grade_sheet_real
              real 
              CHECK (value >= 0.0
                     AND value <= 100.00);

CREATE TYPE grade_sheet AS
            (subjectname varchar(100),
             teachername varchar(100),
             uti varchar(32),
             markallocated grade_sheet_real,
             markobtained grade_sheet_real,
             gradeobtained char(2),
             datetaken timestamp);