From a1700c8b1c68c59ba0452829113772c22a1cf17d Mon Sep 17 00:00:00 2001 From: Daniel Bernstein Date: Tue, 19 Nov 2024 10:08:27 -0800 Subject: [PATCH] Make loan.patron_id and hold.patron_id fields non-nullable. --- ...make_loan_patron_id_and_hold_patron_id_.py | 43 +++++++++++++++++++ src/palace/manager/sqlalchemy/model/patron.py | 4 +- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 alembic/versions/20241119_58b0ae7f5b67_make_loan_patron_id_and_hold_patron_id_.py diff --git a/alembic/versions/20241119_58b0ae7f5b67_make_loan_patron_id_and_hold_patron_id_.py b/alembic/versions/20241119_58b0ae7f5b67_make_loan_patron_id_and_hold_patron_id_.py new file mode 100644 index 000000000..cfd409637 --- /dev/null +++ b/alembic/versions/20241119_58b0ae7f5b67_make_loan_patron_id_and_hold_patron_id_.py @@ -0,0 +1,43 @@ +"""Make Loan.patron_id and Hold.patron_id non-nullable. + +Revision ID: 58b0ae7f5b67 +Revises: 272da5f400de +Create Date: 2024-11-19 18:04:24.182444+00:00 + +""" + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "58b0ae7f5b67" +down_revision = "272da5f400de" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.alter_column( + table_name="loans", + column_name="patron_id", + nullable=False, + ) + + op.alter_column( + table_name="holds", + column_name="patron_id", + nullable=False, + ) + + +def downgrade() -> None: + op.alter_column( + table_name="loans", + column_name="patron_id", + nullable=True, + ) + + op.alter_column( + table_name="holds", + column_name="patron_id", + nullable=True, + ) diff --git a/src/palace/manager/sqlalchemy/model/patron.py b/src/palace/manager/sqlalchemy/model/patron.py index 66d06d5a6..ac5e3ec0e 100644 --- a/src/palace/manager/sqlalchemy/model/patron.py +++ b/src/palace/manager/sqlalchemy/model/patron.py @@ -512,7 +512,7 @@ class Loan(Base, LoanAndHoldMixin): __tablename__ = "loans" id = Column(Integer, primary_key=True) - patron_id = Column(Integer, ForeignKey("patrons.id"), index=True) + patron_id = Column(Integer, ForeignKey("patrons.id"), index=True, nullable=False) patron: Mapped[Patron] = relationship("Patron", back_populates="loans") # A Loan is always associated with a LicensePool. @@ -562,7 +562,7 @@ class Hold(Base, LoanAndHoldMixin): __tablename__ = "holds" id = Column(Integer, primary_key=True) - patron_id = Column(Integer, ForeignKey("patrons.id"), index=True) + patron_id = Column(Integer, ForeignKey("patrons.id"), index=True, nullable=False) license_pool_id = Column(Integer, ForeignKey("licensepools.id"), index=True) license_pool: Mapped[LicensePool] = relationship( "LicensePool", back_populates="holds"