How to Index a field in ERP Next doctype

in ERPNext 10
we have the facility to index a field to optimize search

in erpnext12 we don’t have field(checkbox) for index. along with unique, mandatory,

index is missing in v12 how to index a field

Thanks in ADvance.

One way to achieve this is by editing the Document class for your DocType:

class Foo(Document):
	""" Document class for DocType 'Foo' """
        ....

def on_doctype_update():
	"""
        When DocType definition is updated, create SQL indexes and constraints.
        """
	frappe.db.add_index("Foo", ["column_bar", "column_baz"])
	frappe.db.add_unique("Foo", ["column_bar"])

can you help me how to use this code??

Please help me how to use this…

def on_doctype_update():
“”"
When DocType definition is updated, create SQL indexes and constraints.
“”"
frappe.db.add_index(“Foo”, [“column_bar”, “column_baz”])
frappe.db.add_unique(“Foo”, [“column_bar”])

AFAIK, on_doctype_update only works if you edit the doctype directly. It didn’t work for me in Customize Form.
Here’s how I added my indices:

Create a file in my_app, I called it after_migration.py.

import frappe

def add_indices():
    indices = {
        "doctype": ["field1", "field2", "field3"]
    }
    for doctype, fields in indices.items():
        frappe.db.add_index(doctype, fields)

Then inside hooks.py add this line:

after_migrate = [
    "my_app.after_migrate.add_indices"
]

With that, your indices will be added the next time you run

bench migrate
1 Like