# Buggy code
def make_student_group():
    for d in frappe.db.get_list("Academic Term"):
        sg_tool = frappe.new_doc("Student Group Creation Tool")
        sg_tool.academic_year = "2016-17"
        sg_tool.academic_term = d.name
        sg_tool.courses = sg_tool.get_courses()
        sg_tool.create_student_groups()
        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_student_group():
    for term in frappe.db.get_list("Academic Term"):
        for program in frappe.db.get_list("Program"):
            sg_tool = frappe.new_doc("Student Group Creation Tool")
            sg_tool.academic_year = "2017-18"
            sg_tool.academic_term = term.name
            sg_tool.program = program.name
            for d in sg_tool.get_courses():
                d = frappe._dict(d)
                student_group = frappe.new_doc("Student Group")
                student_group.student_group_name = d.student_group_name
                student_group.group_based_on = d.group_based_on
                student_group.program = program.name
                student_group.course = d.course
                student_group.batch = d.batch
                student_group.academic_term = term.name
                student_group.academic_year = "2017-18"
                student_group.save()
            frappe.db.commit()