Skip to content

Commit b696df9

Browse files
authored
fix: 修复 postgresql 下关系名过长的问题 (#160)
`postgresql` 中关系名不能超过 63 字节,所以删除 `init_db` 中创建约束的命令 fixed #159
1 parent 6ffb3a8 commit b696df9

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- 修复 postgresql 下关系名过长的问题
13+
1014
## [0.4.2] - 2024-08-19
1115

1216
### Fixed

nonebot_plugin_user/migrations/9492159f98f7_remove_foreignkey.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from collections.abc import Sequence
1212

1313
from alembic import op
14+
from nonebot import logger
15+
from sqlalchemy.exc import IdentifierError
1416

1517
revision: str = "9492159f98f7"
1618
down_revision: str | Sequence[str] | None = "ac57f7074e58"
@@ -22,35 +24,28 @@ def upgrade(name: str = "") -> None:
2224
if name:
2325
return
2426
# ### commands auto generated by Alembic - please adjust! ###
25-
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
26-
batch_op.drop_constraint(
27-
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
28-
type_="foreignkey",
29-
)
30-
batch_op.drop_constraint(
31-
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
32-
type_="foreignkey",
33-
)
3427

28+
# 尝试删除外键约束
29+
# 老的数据库中还有约束,但是新的没有,所以这里可能会报错
30+
try:
31+
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
32+
batch_op.drop_constraint(
33+
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
34+
type_="foreignkey",
35+
)
36+
batch_op.drop_constraint(
37+
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
38+
type_="foreignkey",
39+
)
40+
logger.info("[user] 已成功删除外键约束")
41+
except (ValueError, IdentifierError):
42+
logger.debug("[user] 未找到外键约束,跳过删除")
3543
# ### end Alembic commands ###
3644

3745

3846
def downgrade(name: str = "") -> None:
3947
if name:
4048
return
4149
# ### commands auto generated by Alembic - please adjust! ###
42-
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
43-
batch_op.create_foreign_key(
44-
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
45-
"nonebot_plugin_user_user",
46-
["bind_id"],
47-
["id"],
48-
)
49-
batch_op.create_foreign_key(
50-
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
51-
"nonebot_plugin_user_user",
52-
["original_id"],
53-
["id"],
54-
)
5550

5651
# ### end Alembic commands ###

nonebot_plugin_user/migrations/ac57f7074e58_init_db.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ def upgrade(name: str = "") -> None:
3737
sa.Column("platform_id", sa.String(length=64), nullable=False),
3838
sa.Column("bind_id", sa.Integer(), nullable=False),
3939
sa.Column("original_id", sa.Integer(), nullable=False),
40-
sa.ForeignKeyConstraint(
41-
["bind_id"],
42-
["nonebot_plugin_user_user.id"],
43-
name=op.f("fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user"),
44-
),
45-
sa.ForeignKeyConstraint(
46-
["original_id"],
47-
["nonebot_plugin_user_user.id"],
48-
name=op.f(
49-
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user"
50-
),
51-
),
5240
sa.PrimaryKeyConstraint(
5341
"platform", "platform_id", name=op.f("pk_nonebot_plugin_user_bind")
5442
),

0 commit comments

Comments
 (0)