Skip to content

Commit

Permalink
fix: 修复 postgresql 下关系名过长的问题 (#160)
Browse files Browse the repository at this point in the history
`postgresql` 中关系名不能超过 63 字节,所以删除 `init_db` 中创建约束的命令

fixed #159
  • Loading branch information
he0119 authored Aug 27, 2024
1 parent 6ffb3a8 commit b696df9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

## [Unreleased]

### Fixed

- 修复 postgresql 下关系名过长的问题

## [0.4.2] - 2024-08-19

### Fixed
Expand Down
39 changes: 17 additions & 22 deletions nonebot_plugin_user/migrations/9492159f98f7_remove_foreignkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from collections.abc import Sequence

from alembic import op
from nonebot import logger
from sqlalchemy.exc import IdentifierError

revision: str = "9492159f98f7"
down_revision: str | Sequence[str] | None = "ac57f7074e58"
Expand All @@ -22,35 +24,28 @@ def upgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
batch_op.drop_constraint(
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
type_="foreignkey",
)
batch_op.drop_constraint(
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
type_="foreignkey",
)

# 尝试删除外键约束
# 老的数据库中还有约束,但是新的没有,所以这里可能会报错
try:
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
batch_op.drop_constraint(
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
type_="foreignkey",
)
batch_op.drop_constraint(
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
type_="foreignkey",
)
logger.info("[user] 已成功删除外键约束")
except (ValueError, IdentifierError):
logger.debug("[user] 未找到外键约束,跳过删除")
# ### end Alembic commands ###


def downgrade(name: str = "") -> None:
if name:
return
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("nonebot_plugin_user_bind", schema=None) as batch_op:
batch_op.create_foreign_key(
"fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user",
"nonebot_plugin_user_user",
["bind_id"],
["id"],
)
batch_op.create_foreign_key(
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user",
"nonebot_plugin_user_user",
["original_id"],
["id"],
)

# ### end Alembic commands ###
12 changes: 0 additions & 12 deletions nonebot_plugin_user/migrations/ac57f7074e58_init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ def upgrade(name: str = "") -> None:
sa.Column("platform_id", sa.String(length=64), nullable=False),
sa.Column("bind_id", sa.Integer(), nullable=False),
sa.Column("original_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(
["bind_id"],
["nonebot_plugin_user_user.id"],
name=op.f("fk_nonebot_plugin_user_bind_bind_id_nonebot_plugin_user_user"),
),
sa.ForeignKeyConstraint(
["original_id"],
["nonebot_plugin_user_user.id"],
name=op.f(
"fk_nonebot_plugin_user_bind_original_id_nonebot_plugin_user_user"
),
),
sa.PrimaryKeyConstraint(
"platform", "platform_id", name=op.f("pk_nonebot_plugin_user_bind")
),
Expand Down

0 comments on commit b696df9

Please sign in to comment.