解决父资源的错误Resolve errors for parent resources

本文介绍部署依赖于父资源的资源时可能遇到的错误。This article describes the errors you may get when deploying a resource that is dependent on a parent resource.

症状Symptom

在部署的资源是另一个资源的子级时,可能会收到以下错误:When deploying a resource that is a child to another resource, you may receive the following error:

Code=ParentResourceNotFound;
Message=Can not perform requested operation on nested resource. Parent resource 'exampleserver' not found."

原因Cause

如果一个资源是另一个资源的子级,则在创建子资源之前,父资源必须存在。When one resource is a child to another resource, the parent resource must exist before creating the child resource. 子资源的名称定义与父资源的连接。The name of the child resource defines the connection with the parent resource. 子资源名称采用格式 <parent-resource-name>/<child-resource-name>The name of the child resource is in the format <parent-resource-name>/<child-resource-name>. 例如,SQL 数据库可能定义为:For example, a SQL Database might be defined as:

{
  "type": "Microsoft.Sql/servers/databases",
  "name": "[concat(variables('databaseServerName'), '/', parameters('databaseName'))]",
  ...

如果在同一模板中部署服务器和数据库,但未在服务器上指定依赖关系,则可以在部署服务器之前启动数据库部署。If you deploy both the server and the database in the same template, but don't specify a dependency on the server, the database deployment might start before the server has deployed.

如果父资源已存在且未部署在同一模板中,则当资源管理器无法将子资源与父资源关联时,会出现此错误。If the parent resource already exists and isn't deployed in the same template, you get this error when Resource Manager can't associate the child resource with parent. 当子资源的格式不正确,或者子资源部署到与父资源的资源组不同的资源组时,可能会发生此错误。This error might happen when the child resource isn't in the correct format, or the child resource is deployed to a resource group that is different than the resource group for parent resource.

解决方案Solution

要在同一模板中部署父资源和子资源时解决此错误,请包含依赖关系。To resolve this error when parent and child resources are deployed in the same template, include a dependency.

"dependsOn": [
    "[variables('databaseServerName')]"
]

要在父资源先前部署在其他模板中时解决此错误,请不要设置依赖关系。To resolve this error when the parent resource was previously deployed in a different template, you don't set a dependency. 而是将子资源部署到同一资源组并提供父资源的名称。Instead, deploy the child to the same resource group and provide the name of the parent resource.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "sqlServerName": {
            "type": "string"
        },
        "databaseName": {
            "type": "string"
        }
    },
    "resources": [
        {
            "apiVersion": "2014-04-01",
            "type": "Microsoft.Sql/servers/databases",
            "location": "[resourceGroup().location]",
            "name": "[concat(parameters('sqlServerName'), '/', parameters('databaseName'))]",
            "properties": {
                "collation": "SQL_Latin1_General_CP1_CI_AS",
                "edition": "Basic"
            }
        }
    ],
    "outputs": {}
}

有关详细信息,请参阅定义 Azure 资源管理器模板中部署资源的顺序For more information, see Define the order for deploying resources in Azure Resource Manager templates.