Add media model, fix a bug when foreign key being deleted