The way I'd do this, is by separating concerns into separate tables. If you have a table with (id, name) and a table with (id, parent_id), any doc with a parent will have a corresponding record in the second table.
Interesting! But where can you implement the UNIQUE constraint for doc names under a given parent? I guess your application code would need to handle that