P Channel MOSFET to do automatic switch between battery and power source
You made two oopsies...


MOSFET is upside down so it will conduct through the internal diode always.
You need to connect the gate to the other side of D1 so it is pulled low when V1 drops. 








simulate this circuit &ndash; Schematic created using CircuitLab

I would have drawn it more like this for clarity though.







simulate this circuit

Be Aware, the 12V for V1 can try to charge the battery through M1 with this circuit though.