How can I get th SQL query of a db method like exists

Is there a way to get the raw SQL of a db method?
For example this method:

def before_submit(self):
	exists = frappe.db.exists(
		"Library Membership",
		{
			"library_member": self.library_member,
			"docstatus": DocStatus.submitted(),
			# check if the membership's end date is later than this membership's start date
			"to_date": (">", self.from_date),
		},
	)
	if exists:
		frappe.throw("There is an active membership for this member")

How can I get the raw SQL of the exists?
Also, can someone explain to me how this method: exists works? What does mean the dict (the second passed argument to the method) does the method do an AND between the fields, and how to change it if I want to do an OR between the fields?

Thanks in advance.

update the docstaus line and check it

def before_submit(self):
    exists = frappe.db.exists(
        "Library Membership",
        {
            "library_member": self.library_member,
            "docstatus": 1,  # Submitted document status
            "to_date": (">", self.from_date),
        },
    )
    if exists:
        frappe.throw(_("There is an active membership for this member"))

Please check the documentation.

Where can I see the SQL statement?

Please check the documentation.

The posted link doesn’t provide any information on how to get the SQL of the exists method or other db methods. It provides information how write raw SQL queries.

What I need is to get the SQL query that the exists method (in the example above) consructs when querying the database.

Hi,

if you open a bench console for the site and type: frappe.db.exist?? , the path to the source will return at the end. Maybe taking a look at that will help?

I dont know if exist method have debug parameter or nit if it has then you can pass debug=true, else there is one more way but in this you will get all the queries which are running in frappe

make this debug true by default in databse.py file