# Buggy code
def make_fees_structure():
    for d in frappe.db.get_list("Program"):
        program = frappe.get_doc("Program", d.name)
        for academic_term in ["Semester 1", "Semester 2", "Semester 3"]:
            fee_structure = frappe.new_doc("Fee Structure")
            fee_structure.program = d.name
            fee_structure.academic_term = random.choice(
                frappe.db.get_list("Academic Term")
            ).name
            for j in range(1, 4):
                temp = {
                    "fees_category": random.choice(
                        frappe.db.get_list("Fee Category")
                    ).name,
                    "amount": random.randint(500, 1000),
                }
                fee_structure.append("components", temp)
            fee_structure.insert()
            program.append(
                "fees",
                {
                    "academic_term": academic_term,
                    "fee_structure": fee_structure.name,
                    "amount": fee_structure.total_amount,
                },
            )
        program.save()
    frappe.db.commit()


# Traceback (frappe.exceptions.LinkValidationError)
➜  frappe-bench bench --site school.erpnext.dev execute erpnext.demo.demo.make --args Education
Complete Setup...
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 79, in <module>
main()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 16, in main
click.Group(commands=commands)(prog_name='bench')
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 716, in __call__
return self.main(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 696, in main
rv = self.invoke(ctx)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 1060, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/env/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/commands/__init__.py", line 24, in _func
ret = f(frappe._dict(ctx.obj), *args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/commands/utils.py", line 110, in execute
ret = frappe.get_attr(method)(*args, **kwargs)
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/demo.py", line 32, in make
education.setup_data()
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/education.py", line 12, in setup_data
make_masters()
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/education.py", line 26, in make_masters
import_json("Program")
File "/Users/rushabh/frappe/frappe-bench/apps/erpnext/erpnext/demo/setup/setup_data.py", line 356, in import_json
doc.insert()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 213, in insert
self._validate()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 388, in _validate
self._validate_links()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/model/document.py", line 630, in _validate_links
frappe.LinkValidationError)
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 302, in throw
msgprint(msg, raise_exception=exc, title=title, indicator='red')
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 295, in msgprint
_raise_exception()
File "/Users/rushabh/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 268, in _raise_exception
raise raise_exception, encode(msg)
frappe.exceptions.LinkValidationError: Could not find Row #1: Academic Term: Semester 1, Row #2: Academic Term: Semester 1, Row #3: Academic Term: Semester 2

# Fixed code
def make_fees_structure():
    for d in frappe.db.get_list("Program"):
        program = frappe.get_doc("Program", d.name)
        for academic_term in [
            "2017-18 (Semester 1)",
            "2017-18 (Semester 2)",
            "2017-18 (Semester 3)",
        ]:
            fee_structure = frappe.new_doc("Fee Structure")
            fee_structure.program = d.name
            fee_structure.academic_term = random.choice(
                frappe.db.get_list("Academic Term")
            ).name
            for j in range(1, 4):
                temp = {
                    "fees_category": random.choice(
                        frappe.db.get_list("Fee Category")
                    ).name,
                    "amount": random.randint(500, 1000),
                }
                fee_structure.append("components", temp)
            fee_structure.insert()
            program.append(
                "fees",
                {
                    "academic_term": academic_term,
                    "fee_structure": fee_structure.name,
                    "amount": fee_structure.total_amount,
                },
            )
        program.save()
    frappe.db.commit()