Amendments in matters of substance may be permitted in any action, in any stage of the proceedings, upon such terms as the court shall deem just and reasonable, when it shall appear to the court that it is necessary for the prevention of injustice; but the rights of third persons shall not be affected thereby.