Migrate SQL to models