【发布时间】:2021-11-23 22:30:02
【问题描述】:
databaseChangeLog:
- changeSet:
id: ...
author: ...
preConditions:
- onError: MARK_RAN
- not:
- tableExists:
tableName: ORDERS
changes:
- createTable:
tableName: ORDERS
columns:
- column:
name: ID
type: INT
autoIncrement: true
constraints:
primaryKey: true
- column:
name: ID_USER
type: INT
...
- sql:
"DELIMITER $$
CREATE FUNCTION autoInc ()
RETURNS INT(10)
BEGIN
DECLARE getCount INT(10);
SET getCount = (
SELECT COUNT(USER_ID)
FROM ORDERS) + 1;
RETURN getCount;
END$$
DELIMITER ;"
我正在使用这种方法。当我在订单中插入新条目时,它也应该增加 user_id。因为我需要 user_id 作为另一个自动增量列。我正在使用 Liquibase 迁移来创建此表,但出现错误:
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ CREATE FUNCTION autoInc () RETURNS INT(10) BEGIN DECLARE getCount I' at line 1
...
也许,我做错了什么?
【问题讨论】:
-
MySQL 不支持每个表有多个自增列。据我所知,只有 PostgreSQL 可以。我不明白你为什么需要这个,但如果你真的需要,我可以看到的选项是创建一个带有一些生成逻辑的“生成”列,或者使用触发器来填充第二列的值。
标签: mysql sql exception syntax-error liquibase