补充用户默认酒店唯一约束
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
-- M003 登录权限底座:数据库级约束普通用户只能有一个默认酒店。
|
||||
-- 上线前如已有脏数据,同一用户多个默认酒店时保留 id 最大的一条,其余改为非默认。
|
||||
UPDATE platform_user_hotel
|
||||
SET default_hotel = 0
|
||||
WHERE default_hotel = 1
|
||||
AND id NOT IN (
|
||||
SELECT keep_id
|
||||
FROM (
|
||||
SELECT MAX(id) AS keep_id
|
||||
FROM platform_user_hotel
|
||||
WHERE default_hotel = 1
|
||||
GROUP BY user_id
|
||||
) kept_defaults
|
||||
);
|
||||
|
||||
-- default_hotel_user_id 是默认酒店唯一约束辅助字段,default_hotel=1 时等于用户 ID,否则为空。
|
||||
ALTER TABLE platform_user_hotel
|
||||
ADD COLUMN default_hotel_user_id BIGINT GENERATED ALWAYS AS (
|
||||
CASE WHEN default_hotel = 1 THEN user_id ELSE NULL END
|
||||
);
|
||||
|
||||
-- MySQL 唯一索引允许多个 NULL,因此只会限制同一用户最多一条 default_hotel=1。
|
||||
CREATE UNIQUE INDEX uk_platform_user_hotel_one_default
|
||||
ON platform_user_hotel (default_hotel_user_id);
|
||||
Reference in New Issue
Block a user