LEFT JOIN auth_user AS au ON au.username=rp.userid
                        WHERE rp.level=2 AND rp.repository=r.repoid),
                 CASE WHEN EXISTS
-                       (SELECT * FROM remoterepositories WHERE repository_id=repositories.id)
-                THEN 1 ELSE 09 END
+                       (SELECT * FROM remoterepositories WHERE remoterepositories.id=r.remoterepository_id)
+                THEN 1 ELSE 0 END
 FROM repositories AS r WHERE approved ORDER BY name""")
                f = open("%s.tmp" % self.conf.get("paths", "gitweblist"), "w")
                for name, anon, web, description, initialclone, owner, remoterepo in curs: