 CREATE FUNCTION directory.tag_entity(entity_id integer, tag character varying)
     RETURNS integer
 AS $$
-    INSERT INTO directory.entitytaglink(tag_id, entity_id)
+    INSERT INTO directory.entity_tag_link(tag_id, entity_id)
     SELECT id, $1
     FROM directory.tag
-    LEFT JOIN directory.entitytaglink ON entitytaglink.tag_id = tag.id AND entitytaglink.entity_id = $1
-    WHERE name = $2 AND entitytaglink.entity_id IS NULL;
+    LEFT JOIN directory.entity_tag_link ON entity_tag_link.tag_id = tag.id AND entity_tag_link.entity_id = $1
+    WHERE name = $2 AND entity_tag_link.entity_id IS NULL;
 
     SELECT $1;
 $$ LANGUAGE sql VOLATILE;

CREATE FUNCTION directory.tag_entity(dn character varying, tag character varying)
    RETURNS character varying
AS $$
    INSERT INTO directory.entitytaglink(tag_id, entity_id)
    SELECT
        f.tag_id,
        f.entity_id

        FROM directory.tag, directory.entity
        WHERE tag.name = $2 AND entity.dn = $1
    ) f
    LEFT JOIN directory.entity_tag_link ON entity_tag_link.tag_id = f.tag_id AND entity_tag_link.entity_id = f.entity_id
    WHERE entity_tag_link.entity_id IS NULL;

    SELECT $1;
$$ LANGUAGE sql VOLATILE;
