ceiling light where stud is
You'll want to use a saddle mount ceiling box with at least 11.25in3 of space for this application as pancake boxes don't have enough room for 2 incoming 12/2 W/G cables -- they don't even have 10in3 of space, typically.  (Image below is simply representative.)