Force create foreign key even if reference does not exist
As a quick fix. you can add a semaphore that prevents the creation of bar before foo exists. Pseudocode:

WHILE true:
    n = (select count(1) 
         from information_schema.tables 
         where table_name = 'foo')
    if n == 1:
        break
    sleep 1

create table bar ...  


In the long run, consider how you should handle dependencies between different parts of the system, that are created independently of each other