Skip to content

Commit 38f49f2

Browse files
committed
Fix attribute error when saving mod that was in deleted modpack
1 parent 453f8d5 commit 38f49f2

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

KerbalStuff/objects.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,12 @@ def __repr__(self) -> str:
274274
class ModListItem(Base): # type: ignore
275275
__tablename__ = 'modlistitem'
276276
id = Column(Integer, primary_key=True)
277-
mod_id = Column(Integer, ForeignKey('mod.id'))
278-
mod = relationship('Mod', backref='mod_list_items')
279-
mod_list_id = Column(Integer, ForeignKey('modlist.id'))
277+
mod_id = Column(Integer, ForeignKey('mod.id', ondelete='CASCADE'), nullable=False)
278+
mod = relationship('Mod', backref=backref('mod_list_items', passive_deletes=True))
279+
mod_list_id = Column(Integer, ForeignKey('modlist.id', ondelete='CASCADE'), nullable=False)
280280
mod_list = relationship('ModList',
281-
backref=backref('mods', order_by="asc(ModListItem.sort_index)"))
282-
sort_index = Column(Integer, default=0)
281+
backref=backref('mods', passive_deletes=True, order_by="asc(ModListItem.sort_index)"))
282+
sort_index = Column(Integer, default=0, nullable=False)
283283

284284
def __repr__(self) -> str:
285285
return '<ModListItem %r %r>' % (self.mod_id, self.mod_list_id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Make most of modlistitem non-nullable
2+
3+
Revision ID: 1cbfc1acd83d
4+
Revises: 833ba1ee8c72
5+
Create Date: 2021-07-30 16:16:44
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = '1cbfc1acd83d'
11+
down_revision = '833ba1ee8c72'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
16+
17+
def upgrade() -> None:
18+
# Remove broken rows
19+
op.execute("DELETE FROM modlistitem WHERE mod_id is NULL OR mod_list_id is NULL OR sort_index is NULL")
20+
21+
# Make most of the columns non-nullable
22+
op.alter_column('modlistitem', 'mod_id', existing_type=sa.INTEGER(), nullable=False)
23+
op.alter_column('modlistitem', 'mod_list_id', existing_type=sa.INTEGER(), nullable=False)
24+
op.alter_column('modlistitem', 'sort_index', existing_type=sa.INTEGER(), nullable=False)
25+
26+
# Cascade deletion for the mod and mod list
27+
op.drop_constraint('modlistitem_mod_list_id_fkey', 'modlistitem', type_='foreignkey')
28+
op.drop_constraint('modlistitem_mod_id_fkey', 'modlistitem', type_='foreignkey')
29+
op.create_foreign_key('modlistitem_mod_list_id_fkey', 'modlistitem', 'modlist', ['mod_list_id'], ['id'], ondelete='CASCADE')
30+
op.create_foreign_key('modlistitem_mod_id_fkey', 'modlistitem', 'mod', ['mod_id'], ['id'], ondelete='CASCADE')
31+
32+
33+
def downgrade() -> None:
34+
op.drop_constraint('modlistitem_mod_id_fkey', 'modlistitem', type_='foreignkey')
35+
op.drop_constraint('modlistitem_mod_list_id_fkey', 'modlistitem', type_='foreignkey')
36+
op.create_foreign_key('modlistitem_mod_id_fkey', 'modlistitem', 'mod', ['mod_id'], ['id'])
37+
op.create_foreign_key('modlistitem_mod_list_id_fkey', 'modlistitem', 'modlist', ['mod_list_id'], ['id'])
38+
op.alter_column('modlistitem', 'sort_index', existing_type=sa.INTEGER(), nullable=True)
39+
op.alter_column('modlistitem', 'mod_list_id', existing_type=sa.INTEGER(), nullable=True)
40+
op.alter_column('modlistitem', 'mod_id', existing_type=sa.INTEGER(), nullable=True)

0 commit comments

Comments
 (0)