Local criteria of flat modules Let $A$, $B$ be rings and $M$ be a $B$-module. Let $f:A \to B$ be a ring morphism. For a prime ideal $p \subset B$ let $q=f^{-1}(p)$, and the corresponding local morphism $A_q \to B_p$ makes $M_p$ an $A_q$-module.
I want to show: If for any prime ideal $p \subset B$, $M_p$ is a flat $A_q$-module, then $M$ is a flat $A$-module.
I want to use the fact "$M$ is flat over $A$ $\iff$ $M_q$ is flat over $A_q$ for all the primes $q \subset A$".
But I have difficulties in two places:
(1) In the above problem, I have $M_p$ rather than $M_q$, and it seems that they may not equal to each other.
(2) $q$ may not be chosen for all the primes in $A$.
 A: Fix a prime ideal $q$ of $A$. Denote by $S=f(A\setminus q)$. Then $B_q=S^{-1}B$,  $M_q:=S^{-1}M$. 
For all prime ideals $p'$ of $B$ such that $p'\cap S=\emptyset$, we have $M_{p'}=M_q\otimes_{B_q} {B_{p'}}$ and it is flat over $A_{q'}$ where $q'=f^{-1}(p')\subseteq q$. As $A_q\to A_{q'}$ is flat (it is a localization map), $M_{p'}$ is flat over $A_{q}$.  
As the prime ideals of $B_q$ correspond canonically to the $p$' as above, we check easily that the localization of $M_q$ at all prime ideals of $B_q$ are flat over $A_q$. So $M_q$ is flat over $A_q$.
Edit Replacing $A\to B$ with $A_q\to B_q$, we are in the next situation: 

Lemma Let $A\to B$ be a ring homomorphism and let $M$ be a $B$ module such that for all prime ideals $p$ of $B$, $M_p$ is flat over $A$. Then $M$ is flat over $A$. 

Proof: Let $M_1\to M_2$ be an injective $A$-linear map and let $K$ be the kernel of $M_1\otimes_A M\to M_2\otimes_A M$. Then $K$ is also a $B$-module. As $B\to B_p$ is flat, $K\otimes_B B_p$ is the kernel of $M_1\otimes_A M_p\to M_2\otimes_{A} M_p$, hence equals to $0$. So $K=0$ and $M$ is flat over $A$. 
