summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKojin Nakana <kohanajs@outlook.com>2022-04-28 13:13:00 +0800
committerKojin Nakana <kohanajs@outlook.com>2022-04-28 13:13:00 +0800
commit4f5946d009dd733d3b6aa031d22a328d56dce757 (patch)
tree16885b1755a1f6de1503eaf86e2fc2b7ff113304
parentaf10568da71be5686d77d0f6be32228cef85f602 (diff)
fix: primary key error
-rw-r--r--index.js7
-rw-r--r--tests/parseDDL.test.js12
2 files changed, 12 insertions, 7 deletions
diff --git a/index.js b/index.js
index 87493db..68fa0fb 100644
--- a/index.js
+++ b/index.js
@@ -60,6 +60,7 @@ function getDefaultValue(value, type) {
function parseType(type, useDefaultModel) {
const table = snakeCase(pluralize(type.name.value));
const lines = [];
+ const lastLines = [];
const onDeletes = [];
let updateTrigger = '';
@@ -189,10 +190,14 @@ END;`;
);
if (isPrimary) {
- lines.push(`PRIMARY KEY (${name}${isAutoIncrement ? ' AUTOINCREMENT' : ''})`);
+ lastLines.push(`PRIMARY KEY (${name}${isAutoIncrement ? ' AUTOINCREMENT' : ''})`);
}
});
+ lastLines.forEach(x => {
+ lines.push(x);
+ })
+
const belongs_many = ((belongsManys.length > 0) ? '\n' : '') + belongsManys.join('\n');
const indexes = ((indices.length > 0) ? '\n' : '') + indices.join('\n');
diff --git a/tests/parseDDL.test.js b/tests/parseDDL.test.js
index 73763ae..f88f6e3 100644
--- a/tests/parseDDL.test.js
+++ b/tests/parseDDL.test.js
@@ -358,8 +358,8 @@ type Persons {
const target = `
CREATE TABLE persons(
id INTEGER NOT NULL ,
- PRIMARY KEY (id) ,
- name TEXT
+ name TEXT ,
+ PRIMARY KEY (id)
);
`;
@@ -377,8 +377,8 @@ type Persons {
const target = `
CREATE TABLE persons(
id INTEGER NOT NULL ,
- PRIMARY KEY (id AUTOINCREMENT) ,
- name TEXT
+ name TEXT ,
+ PRIMARY KEY (id AUTOINCREMENT)
);
`;
@@ -396,8 +396,8 @@ type Person {
const target = `
CREATE TABLE persons(
id INTEGER NOT NULL ,
- PRIMARY KEY (id AUTOINCREMENT) ,
- name TEXT
+ name TEXT ,
+ PRIMARY KEY (id AUTOINCREMENT)
);
`;