server/db: fix user cascade deletions

This commit is contained in:
rr- 2016-08-15 18:36:14 +02:00
parent a224297c4f
commit 522886ae6c
3 changed files with 41 additions and 2 deletions

View file

@ -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)

View file

@ -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')

View file

@ -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'])