Fix some bugs and save pm in db to assign an id