Docker compose network issue

I have the following docker-compose.yml file:

version: "3.9"
services:
    result:
        image: result
        ports:
            - "8080:80"
        networks:
            - test-wp-network

And network:

docker network list  | grep test
a18a8c7a2764   test-wp-network   bridge    local

But when I run docker-compose up the following error is shown:

service "db" refers to undefined network test-wp-network: invalid compose project

Could you please clarify what is the issue and how it can be fixed?

For example, if I run it as below:

docker run --name=db -e POSTGRES_PASSWORD=mysecretpassword --network=test-wp-network postgres:9.4

It is working fine

I guess I found the solution it should be as below:

version: "3.9"
services:
    db:
        image: postgres:9.4
        environment:
            - POSTGRES_HOST_AUTH_METHOD=trust
        networks:
            - test-wp-network
    result:
        image: result
        ports:
            - "8080:80"
        networks:
            - test-wp-network
networks:
    test-wp-network:

With the declared network

Yes, you need the definition of the network in the compose file, but docker compose will create an other network with the project name as prefix like: <projectname>_<networkname>. For example: myproject_test-wp-network. It’s fine and usually a good idea, but if you want to use the same network that you created without compose, that is an “external network”: Docker Compose documentation: external network