This issue has been discused several times on various threads in here. In order to create the best possible circumstances you have to create the best possible setup to avoid issues like this one. Acquire a motor with a longer shaft. Create a better situation where the shaft fits better inside the pinion gear. Grind a divit into the shaft giving your setscrew a better seat for keeping the setup together under any conditions. Use a more condusive pinion gear with a larger setscrew so you can place more torque on the setscrew. Use some sort of adhesive to assist the setscrew with holding the pinion gear in palce, like green loctite on the shaft, and red loctite on the setscrew.
Without actually being able to see your setup, nailing exactly what you need to do is difficult at best. Post up some pics of your setup so we can make a better analysis of the situation, and possibly provide a more exact solution.
I use threadlock on the motor shaft as well as the setscrew.
i only use red thread lock and no grub screws, i have never lost a pinion that way, just litely coat the shaft and set the pinion, give it an hour to dry and its set, you will have to use a heat gun or lighter to remove it but it works like magic