Skip to content

Commit c5ba522

Browse files
Merge pull request #2209 from taozhi8833998/fix-not-null-pos-mysql
fix: not null position after generated column in mysql
2 parents c638563 + c2ebdf1 commit c5ba522

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/column.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ function columnOption(definition) {
110110
column_format: columnFormat,
111111
reference_definition: referenceDefinition,
112112
} = definition
113-
114-
columnOpt.push(toUpper(nullable && nullable.action), toUpper(nullable && nullable.value))
113+
const nullSQL = [toUpper(nullable && nullable.action), toUpper(nullable && nullable.value)].filter(hasVal).join(' ')
114+
if (!generated) columnOpt.push(nullSQL)
115115
if (defaultOpt) {
116116
const { type, value } = defaultOpt
117117
columnOpt.push(type.toUpperCase(), exprToSQL(value))
@@ -120,6 +120,7 @@ function columnOption(definition) {
120120
if (constraint) columnOpt.push(toUpper(constraint.keyword), literalToSQL(constraint.constraint))
121121
columnOpt.push(constraintDefinitionToSQL(check))
122122
columnOpt.push(generatedExpressionToSQL(generated))
123+
if (generated) columnOpt.push(nullSQL)
123124
columnOpt.push(autoIncrementToSQL(autoIncrement), toUpper(primaryKey), toUpper(uniqueKey), commentToSQL(comment))
124125
columnOpt.push(...commonTypeValue(characterSet))
125126
if (database !== 'sqlite') columnOpt.push(exprToSQL(collate))

test/create.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('create', () => {
133133

134134
it('should support generated columns', () =>{
135135
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) CHARACTER SET latin1 COLLATE latin1_general_cs AS (CONCAT(first_name,' ',last_name)) STORED NOT NULL);`))
136-
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL AS (CONCAT(`first_name`, ' ', `last_name`)) STORED CHARACTER SET latin1 COLLATE latin1_general_cs)");
136+
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) AS (CONCAT(`first_name`, ' ', `last_name`)) STORED NOT NULL CHARACTER SET latin1 COLLATE latin1_general_cs)");
137137

138138
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) AS (CONCAT(first_name,' ',last_name)) STORED);`))
139139
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) AS (CONCAT(`first_name`, ' ', `last_name`)) STORED)");
@@ -144,7 +144,7 @@ describe('create', () => {
144144

145145
it('should support generated columns with generated always', () =>{
146146
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) CHARACTER SET latin1 COLLATE latin1_general_cs GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)) STORED NOT NULL);`))
147-
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) NOT NULL GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) STORED CHARACTER SET latin1 COLLATE latin1_general_cs)");
147+
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) STORED NOT NULL CHARACTER SET latin1 COLLATE latin1_general_cs)");
148148

149149
expect(getParsedSql(`CREATE TABLE contacts (id INT KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, fullname varchar(101) GENERATED ALWAYS AS (CONCAT(first_name,' ',last_name)) VIRTUAL);`))
150150
.to.equal("CREATE TABLE `contacts` (`id` INT KEY, `first_name` VARCHAR(50) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `fullname` VARCHAR(101) GENERATED ALWAYS AS (CONCAT(`first_name`, ' ', `last_name`)) VIRTUAL)");

test/mysql-mariadb.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,17 @@ describe('mysql', () => {
11371137
'SELECT `4k_pic` FROM `table1`'
11381138
]
11391139
},
1140+
{
1141+
title: 'create table not null position',
1142+
sql: [
1143+
`create table \`d\` (
1144+
\`id\` int (11) primary key auto_increment not null,
1145+
\`d_name\` varchar(15) not null,
1146+
\`d_id\` int(11) generated always as (cast(trim(d_name) as signed) ) virtual not null
1147+
);`,
1148+
'CREATE TABLE `d` (`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `d_name` VARCHAR(15) NOT NULL, `d_id` INT(11) GENERATED ALWAYS AS (CAST(TRIM(`d_name`) AS SIGNED)) VIRTUAL NOT NULL)'
1149+
]
1150+
},
11401151
]
11411152
SQL_LIST.forEach(sqlInfo => {
11421153
const { title, sql } = sqlInfo

0 commit comments

Comments
 (0)