# Buggy code
def as_create_delta(
    self: Object_T,
    schema: s_schema.Schema,
    context: ComparisonContext,
) -> sd.ObjectCommand[Object_T]:
    from . import delta as sd

    cls = type(self)
    delta = self.init_delta_command(
        schema,
        sd.CreateObject,
        canonical=True,
    )

    if context.generate_prompts:
        delta.set_annotation("orig_cmdclass", type(delta))

    # IDs are assigned once when the object is created and
    # never changed.
    id_value = self.get_explicit_field_value(schema, "id")
    delta.set_attribute_value("id", id_value)

    ff = cls.get_fields(sorted=True).items()
    fields = {fn: f for fn, f in ff if f.simpledelta and not f.ephemeral}
    for fn, f in fields.items():
        value = self.get_explicit_field_value(schema, fn, None)

        if (
            value is None
            and context.descriptive_mode
            and (f.describe_visibility & DescribeVisibilityFlags.SHOW_IF_DERIVED)
        ):
            value = self.get_field_value(schema, fn)
            value_from_default = True
        else:
            value_from_default = False

        if f.aux_cmd_data:
            delta.set_object_aux_data(fn, value)
        if value is not None:
            v: Any
            if issubclass(f.type, ObjectContainer):
                v = value.as_shell(schema)
            else:
                v = value
            self.record_field_create_delta(
                schema,
                delta,
                context=context,
                fname=fn,
                value=v,
                from_default=value_from_default,
            )

    for refdict in cls.get_refdicts():
        refcoll: ObjectCollection[Object] = self.get_field_value(schema, refdict.attr)
        sorted_refcoll = sorted(
            refcoll.objects(schema),
            key=lambda o: o.get_name(schema),
        )
        for ref in sorted_refcoll:
            delta.add(ref.as_create_delta(schema, context))

    return delta


# Traceback (AttributeError)
ERROR: InternalServerError: 'UnionType' object has no attribute 'get_name'
Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
Server traceback:
Traceback (most recent call last):
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/server/procpool/worker.py", line 75, in worker
res = await meth(*args)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1808, in compile_eql_tokens
return self._compile(ctx=ctx, tokens=eql_tokens)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1442, in _compile
comp: dbstate.BaseQuery = self._compile_dispatch_ql(ctx, stmt)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 1365, in _compile_dispatch_ql
return self._compile_ql_migration(ctx, ql)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/server/compiler/compiler.py", line 794, in _compile_ql_migration
target_schema = s_ddl.apply_sdl(
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/schema/ddl.py", line 371, in apply_sdl
ddl_stmts = s_decl.sdl_to_ddl(current_schema, documents)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 230, in sdl_to_ddl
trace_dependencies(decl_ast, ctx=ctx)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/functools.py", line 875, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 499, in trace_default
_register_item(node, ctx=ctx)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 623, in _register_item
trace_dependencies(cmd, ctx=ctx)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/functools.py", line 875, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 443, in trace_ConcretePointer
_register_item(
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/declarative.py", line 638, in _register_item
tdeps = qltracer.trace_refs(
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/tracer.py", line 162, in trace_refs
trace(qltree, ctx=ctx)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/functools.py", line 875, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-alpha6/lib/python3.8/site-packages/edb/edgeql/tracer.py", line 653, in trace_Select
ctx.path_prefix = tip.get_name(ctx.schema)
AttributeError: 'UnionType' object has no attribute 'get_name'

# Fixed code
def as_create_delta(
    self: Object_T,
    schema: s_schema.Schema,
    context: ComparisonContext,
) -> sd.ObjectCommand[Object_T]:
    from . import delta as sd

    cls = type(self)
    delta = self.init_delta_command(
        schema,
        sd.CreateObject,
        canonical=True,
    )

    if context.generate_prompts:
        delta.set_annotation("orig_cmdclass", type(delta))

    ff = cls.get_fields(sorted=True).items()
    fields = {fn: f for fn, f in ff if f.simpledelta and not f.ephemeral}
    for fn, f in fields.items():
        value = self.get_explicit_field_value(schema, fn, None)

        if (
            value is None
            and context.descriptive_mode
            and (f.describe_visibility & DescribeVisibilityFlags.SHOW_IF_DERIVED)
        ):
            value = self.get_field_value(schema, fn)
            value_from_default = True
        else:
            value_from_default = False

        if f.aux_cmd_data:
            delta.set_object_aux_data(fn, value)
        if value is not None:
            v: Any
            if issubclass(f.type, ObjectContainer):
                v = value.as_shell(schema)
            else:
                v = value
            self.record_field_create_delta(
                schema,
                delta,
                context=context,
                fname=fn,
                value=v,
                from_default=value_from_default,
            )

    for refdict in cls.get_refdicts():
        refcoll: ObjectCollection[Object] = self.get_field_value(schema, refdict.attr)
        sorted_refcoll = sorted(
            refcoll.objects(schema),
            key=lambda o: o.get_name(schema),
        )
        for ref in sorted_refcoll:
            delta.add(ref.as_create_delta(schema, context))

    return delta