-1

I have an Azure Function app created locally using Python code, SQL Server change tracking that tracks any operation and triggers the SQL Server trigger for further process.

app = func.FunctionApp()    
@app.function_name(name="SqlTrigger")
app.sql_trigger(arg_name="todo",table_name="TableABC", connection_string_setting="conn_string")

Locally, I have a local.setting.json file that contains the conn_string. Everything works fine in local development and testing. i.e I make a change in table, it is captured by the function app and I can see the changes that is forwarded by another process.

Now, I successfully deploy it to Azure Function app from VS Code. It's not working and I don't find the connection_string. I tried the same process of making changes in an Azure SQL table, but the Function app doesn't react to the change.

I have tried adding it manually in the app settings, environment variables, but that didn't help.

Function Code+Test

Connection

3
  • 1
    How do you retrieve environment variables in your code? Commented Jan 23 at 18:47
  • Did you added the connection string in portal of environment variables section? Commented Jan 24 at 2:06
  • @sudip Can you share the full code and local.settings.json file? Commented Jan 24 at 3:31

1 Answer 1

0

Azure function app with SQL Trigger not working after deployment

Initially I also got the same issue. but, after adding the SQL Connection String in Environmental variables section worked as expected. Make sure key is same in both local and portal. check below: Settings>>Environmental variables>>Connection strings

enter image description here

Function code:

app = func.FunctionApp()

@app.function_name(name="GetProducts")
@app.route(route="getproducts/{cost}")
@app.sql_input(arg_name="products",
                        command_text="SELECT * FROM Products WHERE Cost = @Cost",
                        command_type="Text",
                        parameters="@Cost={cost}",
                        connection_string_setting="SqlConnectionString")
def get_products(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse:
    rows = list(map(lambda r: json.loads(r.to_json()), products))

    return func.HttpResponse(
        json.dumps(rows),
        status_code=200,
        mimetype="application/json"
    )

@app.function_name(name="AddProduct")
@app.route(route="addproduct")
@app.sql_output(arg_name="product",
                        command_text="[dbo].[Products]",
                        connection_string_setting="SqlConnectionString")
def add_product(req: func.HttpRequest, product: func.Out[func.SqlRow]) -> func.HttpResponse:
    body = json.loads(req.get_body())
    row = func.SqlRow.from_dict(body)
    product.set(row)

    return func.HttpResponse(
        body=req.get_body(),
        status_code=201,
        mimetype="application/json"
    )

@app.function_name(name="ProductsTrigger")
@app.sql_trigger(arg_name="products",
                        table_name="Products",
                        connection_string_setting="SqlConnectionString")
def products_trigger(products: str) -> None:
    logging.info("SQL Changes: %s", json.loads(products))

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "SqlConnectionString": "Your SQL-Connection-string",
    "PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1"
  }
}

The data which i stored in my database below:

enter image description here

The Function worked fine in local. enter image description here

The function deployed successfully into azure.

enter image description here

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Hi @Pavan. Thanks for sharing the detail process. IN my case, i am just using sql trigger . I have change tracking enabled in azure sql db. Whenever, there is change , the function app should run. I have added the connection string manually without enclosing with "" . But still its same. The session is not starting . The status of function app is running.
can you restart the function app after adding connection string in portal and check once again.
done . thank you. I tested it using http trigger. the connection string is working. However, I found the actual problem. I have enabled change tracking feature in azure sql db tables. This is not being detected in deployed function app. It works perfectly fine in local.
@sudip still getting any issue?
I also enabled change tracking feature in azure sql db tables. after deployment also its working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.