How the "name" in Dynamic Link is generated?

How the “name” field from Dynamic Link is generated?
I can not seem to find the source code.

"link_doctype": "Customer",
		"modified_by": "Administrator",
		**"name": "a6cd82ce69",**
		"parent": "********",
		"creation": "2017-04-20 10:14:36.218537",
		"modified": "2017-08-01 15:26:01.977021",
		"idx": 1,
		"parenttype": "Contact",
		"link_name": "*****",
		"owner": "Administrator",
		"docstatus": 0,
		"parentfield": "links"

Can you explain in details ?

names are created from naming.py base on Autoname Rules

@makarand_b I want to create data on “tabDynamic Link” with sql query and I was wondering how do you generate the “name” on Dynamic Link

@aldoblack,

the name is auto generated for tabDynamic Link doctype so for other doctype from naming.py.

For Dynamic Link the Autoname Rule is hash so system will generate the random hash string and set it as name

instead of inserting record using sql, add the child table row using custom method

@makarand_b I know that. But what if I want to insert it from SQL?

You can certainly use sql queries. In that case, you will have to handle the naming and everything.
Recommended way is to use the frappe apis

Just for the record: have a look at frappe/frappe/model/naming.py.
Look out for

def make_autoname(key="", doctype="", doc=""):

Here you will find

if key == "hash":
		return frappe.generate_hash(doctype, 10)

which will lead you ultimately to

digest = hashlib.sha224(((txt or "") + repr(time.time()) + repr(random_string(8))).encode()).hexdigest()

txt will contain the name of your doctype. digest will then be shortened to the first 10 chars and used as “name”.

So to generate your own names to use in SQL and are not willing to duplicate frappes behaviour:
you could create your own sequence and use the resulting numbers as names

These will not collide with the names generated by frappe.
Didn’t test it, no gurantees.