'sync child table for given fieldname'

def update_child_table(self, fieldname, df=None):

    rows = []
    if (not df):
        df = self.meta.get_field(fieldname)
    for d in self.get(df.fieldname):
        d.db_update()
        rows.append(d.name)
    if (df.options in (self.flags.ignore_children_type or [])):
        return
    if rows:
        deleted_rows = frappe.db.sql(u'select    name    from    `tab{0}`    where    parent=%s\n DCTB  DCTB  DCTB  DCTB and    parenttype=%s    and    parentfield=%s\n DCTB  DCTB  DCTB  DCTB and    name    not    in    ({1})'.format(df.options, u','.join(([u'%s'] * len(rows)))), ([self.name, self.doctype, fieldname] + rows))
        if (len(deleted_rows) > 0):
            frappe.db.sql(u'delete    from    `tab{0}`    where    name    in    ({1})'.format(df.options, u','.join(([u'%s'] * len(deleted_rows)))), tuple((row[0] for row in deleted_rows)))
    else:
        frappe.db.sql(u'delete    from    `tab{0}`    where    parent=%s\n DCTB  DCTB  DCTB  DCTB and    parenttype=%s    and    parentfield=%s'.format(df.options), (self.name, self.doctype, fieldname))
