document_relation.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """方案关联关系模型(方案与其关联方案间的有向声明)。"""
  2. from __future__ import annotations
  3. from sqlalchemy import (
  4. BigInteger,
  5. CheckConstraint,
  6. Computed,
  7. ForeignKey,
  8. Index,
  9. Integer,
  10. String,
  11. UniqueConstraint,
  12. text,
  13. )
  14. from sqlalchemy.orm import Mapped, mapped_column
  15. from dms.database.base import BusinessTableMixin
  16. from dms.extensions import db
  17. class DocumentRelation(BusinessTableMixin, db.Model):
  18. __tablename__ = "doc_document_relation"
  19. __table_args__ = (
  20. CheckConstraint(
  21. "document_id <> related_document_id",
  22. name="document_relation_no_self",
  23. ),
  24. UniqueConstraint(
  25. "document_id",
  26. "related_document_id",
  27. "active_marker",
  28. name="uq_doc_document_relation_active",
  29. ),
  30. Index(
  31. "ix_doc_document_relation_document_active_sort",
  32. "document_id",
  33. "is_deleted",
  34. "sort_no",
  35. ),
  36. Index(
  37. "ix_doc_document_relation_related_active",
  38. "related_document_id",
  39. "is_deleted",
  40. ),
  41. )
  42. document_id: Mapped[int] = mapped_column(
  43. BigInteger,
  44. ForeignKey("doc_document.id", ondelete="RESTRICT"),
  45. nullable=False,
  46. )
  47. related_document_id: Mapped[int] = mapped_column(
  48. BigInteger,
  49. ForeignKey("doc_document.id", ondelete="RESTRICT"),
  50. nullable=False,
  51. )
  52. relation_type: Mapped[str] = mapped_column(String(64), nullable=False)
  53. sort_no: Mapped[int] = mapped_column(
  54. Integer,
  55. nullable=False,
  56. server_default=text("0"),
  57. )
  58. active_marker: Mapped[int | None] = mapped_column(
  59. Integer,
  60. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  61. nullable=True,
  62. )