anchor.diff:          final String sql =
             "SELECT S.* " +
             " FROM " +
  -          "<%=j_escape_string("   #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS S ")%>" +
  +          "<%= j_escape_string("   #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS S ") %>" +
   <% if a.referenced_entity.name == entity.name -%>
  -          "<%=j_escape_string(" LEFT JOIN #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS O ON O.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name }")%>" +
  +          "<%= j_escape_string(" LEFT JOIN #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS O ON O.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name }") %>" +
   <% elsif a.referenced_entity.abstract? -%>
   <% a.referenced_entity.concrete_subtypes.each_with_index do |subtype, index| -%>
  -          "<%=j_escape_string(" LEFT JOIN #{ subtype.sync.master_entity.sql.qualified_table_name } AS O#{ index } ON O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL")%>" +
  +          "<%= j_escape_string(" LEFT JOIN #{ subtype.sync.master_entity.sql.qualified_table_name } AS O#{ index } ON O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL") %>" +
   <% end -%>
   <% else -%>
  -          "<%=j_escape_string(" LEFT JOIN #{ a.referenced_entity.sync.master_entity.sql.qualified_table_name } AS O ON O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL")%>" +
  -<% end -%>
  -          "<%=j_escape_string(" WHERE S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } IS NOT NULL AND S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = ? AND ")%>" +
  +          "<%= j_escape_string(" LEFT JOIN #{ a.referenced_entity.sync.master_entity.sql.qualified_table_name } AS O ON O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND O.#{ a.referenced_entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL") %>" +
  +<% end -%>
  +          "<%= j_escape_string(" WHERE S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } IS NOT NULL AND S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = ? AND ") %>" +
   <% if a.referenced_entity.abstract? -%>
             "(" +
   <% a.referenced_entity.concrete_subtypes.each_with_index do |subtype, index| -%>
  -            "<%=j_escape_string("O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } IS NULL AND ")%>" +
  +            "<%= j_escape_string("O#{ index }.#{ subtype.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } IS NULL AND ") %>" +
   <% end -%>
             " 1 = 1 )";
   <% else -%>
  -          "<%=j_escape_string(" O.#{ entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } IS NULL")%>";
  +          "<%= j_escape_string(" O.#{ entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } IS NULL") %>";
   <% end -%>
           if ( !verify<%= entity.qualified_name.gsub('.','') %>ViaSql( mappingSourceCode, sql, "<%= a.name %> ForeignKey", recorder ) )
           {
  
positive.code:         final String sql =
            "SELECT S.* " +
            " FROM " +
            "<%=j_escape_string("   #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS S ")%>" +
  <% if a.reference?
    ref_id = 'R.ID'
  -%>
  <% if a.referenced_entity.name == entity.name -%>
            "<%=j_escape_string(" LEFT JOIN #{ a.referenced_entity.sync.sync_temp_entity.sql.qualified_table_name } AS R ON R.#{ entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND R.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } ")%>" +
  <% elsif a.referenced_entity.abstract? -%>
  <%
    refs = []
    a.referenced_entity.concrete_subtypes.each_with_index do |subtype, index|
      refs << "R#{index}.ID"
  -%>
            "<%=j_escape_string(" LEFT JOIN #{ subtype.sync.master_entity.sql.qualified_table_name } AS R#{ index } ON R#{ index }.#{ entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND R#{ index }.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND R#{ index }.#{ entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL ")%>" +
  <% end
    ref_id = "COALESCE(#{refs.join(',')})"
  -%>
  
  <% else -%>
            "<%=j_escape_string(" LEFT JOIN #{ a.referenced_entity.sync.master_entity.sql.qualified_table_name } AS R ON R.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name("#{a.name}MappingID").sql.quoted_column_name } AND R.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND R.#{ entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL ")%>" +
  <% end -%>
  <% end -%>
            "<%=j_escape_string(" JOIN #{ entity.sync.master_entity.sql.qualified_table_name } AS M ON M.#{ entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name } IS NULL AND M.#{ entity.sync.master_entity.attribute_by_name(:MappingKey).sql.quoted_column_name } = S.#{ entity.sync.master_entity.attribute_by_name(:MappingKey).sql.quoted_column_name } AND M.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name } AND M.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } AND M.#{ entity.sync.master_entity.attribute_by_name(a.name).sql.quoted_column_name } <> #{ a.reference? ? ref_id : "S.#{entity.sync.sync_temp_entity.attribute_by_name(a.reference? ? "#{a.name}MappingID" : a.name).sql.quoted_column_name}" } ")%>" +
            "<%=j_escape_string(" WHERE S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = ?")%>";
          if ( !verify<%= entity.qualified_name.gsub('.','') %>ViaSql( mappingSourceCode, sql, "Attempted to modify immutable attribute <%= a.qualified_name %>", recorder ) )
          {
            validationFailed = true;
  
negative.code:         final String sql =
            "SELECT S.* " +
            " FROM " +
            "<%= j_escape_string("   #{ entity.sync.sync_temp_entity.sql.qualified_table_name } AS S ") %>" +
            "<%= j_escape_string("JOIN ( SELECT #{ entity.sync.sync_temp_entity.attribute_by_name(:SyncTempID).sql.quoted_column_name } FROM #{ entity.sync.sync_temp_entity.sql.qualified_table_name } WHERE NOT (#{
    sql = constraint.sql.gsub("\n",' ').gsub('  ',' ')
    entity.attributes.select{|a| !a.primary_key? && a.sql? && a.name.to_s != 'CreatedAt' && a.name.to_s != 'DeletedAt' }.each do |a|
      sql = sql.gsub(/#{Regexp.escape(a.sql.quoted_column_name)}/, entity.sync.sync_temp_entity.attribute_by_name(a.reference? ? "#{a.name}MappingID" : a.name).sql.quoted_column_name )
    end
    sql
  }) ) J ON J.#{ entity.sync.sync_temp_entity.attribute_by_name(:SyncTempID).sql.quoted_column_name } = S.#{ entity.sync.sync_temp_entity.attribute_by_name(:SyncTempID).sql.quoted_column_name } ") %>" +
          "<%= j_escape_string("WHERE S.#{ entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name } = ?") %>";
          if ( !verify<%= entity.qualified_name.gsub('.','') %>ViaSql( mappingSourceCode, sql, "Attempted to violate constraint <%= j_escape_string( constraint.qualified_constraint_name ) %>", recorder ) )
          {
            validationFailed = true;
  