diff --git a/app/application/site/mutation.py b/app/application/site/mutation.py index d153f56d9..66fc82e25 100644 --- a/app/application/site/mutation.py +++ b/app/application/site/mutation.py @@ -149,6 +149,7 @@ class SiteMutationCommand: if await self._repository.async_get_by_domain(domain): return SiteMutationResult(False, f"{domain} 站点己存在") + values.pop("id", None) values.update( { "domain": domain, diff --git a/tests/test_site_mutation_command.py b/tests/test_site_mutation_command.py index cd25925b1..be4d77b29 100644 --- a/tests/test_site_mutation_command.py +++ b/tests/test_site_mutation_command.py @@ -4,6 +4,7 @@ import pytest from app.application.site.contract import SiteMutation, SitePriorityMutation from app.application.site.mutation import SiteMutationCommand +from app.schemas.site import Site def _command(**overrides): @@ -56,6 +57,20 @@ async def test_create_site_commits_before_updated_event(): assert mutation.values["public"] == 1 +@pytest.mark.asyncio +async def test_create_site_ignores_schema_identity_field(): + """手动新增站点应忽略请求 Schema 自动补出的空主键。""" + command, dependencies = _command() + + result = await command.create( + Site(url="https://demo.example/path").model_dump() + ) + + assert result.success is True + mutation = dependencies["repository"].stage_create.await_args.args[0] + assert "id" not in mutation.values + + @pytest.mark.asyncio async def test_update_site_returns_legacy_not_found_without_writes(): """更新不存在站点时保持失败响应且不产生事务或事件。"""