# Buggy code
def assign_student_group(student, program):
    courses = []
    for d in frappe.get_list(
        "Program Course", fields=("course"), filters={"parent": program}
    ):
        courses.append(d.course)

    for d in xrange(3):
        course = random.choice(courses)
        random_sg = get_random("Student Group", {"course": course})
        if random_sg:
            student_group = frappe.get_doc("Student Group", random_sg)
            student_group.append("students", {"student": student})
            student_group.save()
        courses.remove(course)


# 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 assign_student_group(student, student_name, program, courses, batch):
    course_list = [d["course"] for d in courses]
    for d in frappe.get_list(
        "Student Group",
        fields=("name"),
        filters={"program": program, "course": ("in", course_list)},
    ):
        student_group = frappe.get_doc("Student Group", d.name)
        student_group.append(
            "students",
            {
                "student": student,
                "student_name": student_name,
                "group_roll_number": len(student_group.students) + 1,
                "active": 1,
            },
        )
        student_group.save()
    student_batch = frappe.get_list(
        "Student Group",
        fields=("name"),
        filters={"program": program, "group_based_on": "Batch", "batch": batch},
    )[0]
    student_batch_doc = frappe.get_doc("Student Group", student_batch.name)
    student_batch_doc.append(
        "students",
        {
            "student": student,
            "student_name": student_name,
            "group_roll_number": len(student_batch_doc.students) + 1,
            "active": 1,
        },
    )
    student_batch_doc.save()
    frappe.db.commit()