diff --git a/server/szurubooru/db/post.py b/server/szurubooru/db/post.py index 741e581c..d68b4e38 100644 --- a/server/szurubooru/db/post.py +++ b/server/szurubooru/db/post.py @@ -136,7 +136,12 @@ class Post(Base): # basic meta post_id = Column('id', Integer, primary_key=True) - user_id = Column('user_id', Integer, ForeignKey('user.id'), index=True) + user_id = Column( + 'user_id', + Integer, + ForeignKey('user.id', ondelete='SET NULL'), + nullable=True, + index=True) version = Column('version', Integer, default=1, nullable=False) creation_time = Column('creation_time', DateTime, nullable=False) last_edit_time = Column('last_edit_time', DateTime) diff --git a/server/szurubooru/db/snapshot.py b/server/szurubooru/db/snapshot.py index 554e11ca..f0a497bd 100644 --- a/server/szurubooru/db/snapshot.py +++ b/server/szurubooru/db/snapshot.py @@ -18,7 +18,8 @@ class Snapshot(Base): resource_id = Column('resource_id', Integer, nullable=False, index=True) resource_repr = Column('resource_repr', Unicode(64), nullable=False) operation = Column('operation', Unicode(16), nullable=False) - user_id = Column('user_id', Integer, ForeignKey('user.id')) + user_id = Column( + 'user_id', Integer, ForeignKey('user.id', ondelete='set null')) data = Column('data', PickleType) user = relationship('User') diff --git a/server/szurubooru/migrations/versions/840b460c5613_fix_foreignkeys.py b/server/szurubooru/migrations/versions/840b460c5613_fix_foreignkeys.py new file mode 100644 index 00000000..8821dd8e --- /dev/null +++ b/server/szurubooru/migrations/versions/840b460c5613_fix_foreignkeys.py @@ -0,0 +1,33 @@ +''' +Fix ForeignKey constraint definitions + +Revision ID: 840b460c5613 +Created at: 2016-08-15 18:39:30.909867 +''' + +import sqlalchemy as sa +from alembic import op + + +revision = '840b460c5613' +down_revision = '7f6baf38c27c' +branch_labels = None +depends_on = None + + +def upgrade(): + op.drop_constraint('post_user_id_fkey', 'post', type_='foreignkey') + op.drop_constraint('snapshot_user_id_fkey', 'snapshot', type_='foreignkey') + op.create_foreign_key( + None, 'post', 'user', ['user_id'], ['id'], ondelete='SET NULL') + op.create_foreign_key( + None, 'snapshot', 'user', ['user_id'], ['id'], ondelete='set null') + + +def downgrade(): + op.drop_constraint(None, 'snapshot', type_='foreignkey') + op.drop_constraint(None, 'post', type_='foreignkey') + op.create_foreign_key( + 'snapshot_user_id_fkey', 'snapshot', 'user', ['user_id'], ['id']) + op.create_foreign_key( + 'post_user_id_fkey', 'post', 'user', ['user_id'], ['id'])