@@ -2179,8 +2179,9 @@ def get_by_metadata(
21792179
21802180 # Format value
21812181 if isinstance (value , str ):
2182- # Escape single quotes in string values
2183- escaped_str = value .replace ("'" , "''" )
2182+ # Escape single quotes using backslash when inside $$ dollar-quoted strings
2183+ # In $$ delimiters, Cypher string literals can use \' to escape single quotes
2184+ escaped_str = value .replace ("'" , "\\ '" )
21842185 escaped_value = f"'{ escaped_str } '"
21852186 elif isinstance (value , list ):
21862187 # Handle list values - use double quotes for Cypher arrays
@@ -4153,6 +4154,17 @@ def _build_filter_conditions_cypher(
41534154 if filter :
41544155
41554156 def escape_cypher_string (value : str ) -> str :
4157+ """
4158+ Escape single quotes in Cypher string literals.
4159+
4160+ In Cypher, single quotes in string literals are escaped by doubling them: ' -> ''
4161+ However, when inside PostgreSQL's $$ dollar-quoted string, we need to be careful.
4162+
4163+ The issue: In $$ delimiters, Cypher still needs to parse string literals correctly.
4164+ The solution: Use backslash escape \' instead of doubling '' when inside $$.
4165+ """
4166+ # Use backslash escape for single quotes inside $$ dollar-quoted strings
4167+ # This works because $$ protects the backslash from PostgreSQL interpretation
41564168 return value .replace ("'" , "\\ '" )
41574169
41584170 def build_cypher_filter_condition (condition_dict : dict ) -> str :
0 commit comments