@@ -824,12 +824,15 @@ def add_canonical_link():
824824
825825 ## check if this link exists already
826826 res = db_session .query (CodeEventCreator , CanonicalEventLink )\
827+ .join (CanonicalEventLink , CodeEventCreator .id == CanonicalEventLink .cec_id )\
827828 .filter (
828829 CodeEventCreator .variable == 'link' ,
829- CodeEventCreator .article_id == article_id
830+ CodeEventCreator .article_id == article_id ,
831+ CanonicalEventLink .canonical_id == canonical_event_id
830832 ).first ()
831833
832- ## if the CEC and CEL are not null, this exists already
834+ ## if the CEC and CEL are not null,
835+ ## and CEL matches canonical event, then link this.
833836 if res and res [0 ] and res [1 ]:
834837 return make_response ("Link already exists." , 400 )
835838
@@ -949,7 +952,8 @@ def del_canonical_link():
949952 for cec in cecs :
950953 ## get the CELs for this CEC
951954 cel = db_session .query (CanonicalEventLink ).filter (CanonicalEventLink .cec_id == cec .id ).first ()
952- db_session .delete (cel )
955+ if cel :
956+ db_session .delete (cel )
953957
954958 ## commit these deletes first to avoid foreign key error
955959 db_session .commit ()
0 commit comments