Sorry, but this deficit it's pretty unique to Spain. There is no country in Europe with this level of immigration.
According to your argument, the answer to when to build is a good moment to build is "never". I can't agree, and there are counter examples too. The mass rural to urban migration happened less than 60 years ago, and the answer wasn't "do nothing".
You are focusing on just one input of the many that happen in a country. Spain has high levels of immigration, other countries have other forms of people movement for different reasons.
Today is also very easy for people to move vast distances, from across town to new countries.
Building new homes and communities where people can live takes years, people can move anywhere in days. The building is slow, movement is fast, the two are out of balance.
According to your argument, the answer to when to build is a good moment to build is "never". I can't agree, and there are counter examples too. The mass rural to urban migration happened less than 60 years ago, and the answer wasn't "do nothing".