Hi All, I’m having a strange problem executing SQL...
# ask-community
m
Hi All, I’m having a strange problem executing SQL command within tasks in a Flow. To summerise, I’m using sqlalchemy to execute those commands for me on the database. the issue is that commands are getting executed with no error returned, but later looking at the database, none of those commands went into effect (as if the were not executed at all). Surprsingly, another task within the same Flow is able to successfully utilize the engine (instantiated the same way), when pandas is using that to dump a dataframe into the database. I did a test on the docker environment, where the tasks are executed, and surprisigly, all those sql command are getting executed successfully. I’m a little puzzled as to why a task can utilize sqlalchemy engine and another task fails to do so? please help.
k
Hey @Mehdi Nazari, can I see the code? Maybe it’s just the return? Like the cursor is not executing?
m
Hey @Kevin Kho, this is the stripped down version after a few adjustment to make sure command are valid.
k
This looks very similar to the Prefect tasks. I don’t see anything wrong. And I guess you said it worked in the Docker environment with the same code? I guess it would be a connection issue then?
m
It did actually, on the same docker container, I tested this code in a python REPL to make sure that environment doesn’t have any problem. they were all successful.
Well that’s where I need help, do you see connection issue? or is it something I’m missing with respect to Prefect environment?
k
I don’t see anything wrong. Why don’t you try
task.run()
so run the task alone without a Flow? And then do you see the logs there? Like the info one.
m
This is a task right before the one above; I’m surprised this succeedes
Ok, where would I do a
task.run()
? command line?
k
Just execute it like python code
load_to_mssql.run()
. This will run the Python code underneath
m
So strange!
task.run()
executes successfully from my laptop! does that tell you something?
k
It doesn’t… cuz that’s just what the Flow does! If you see the task running and you see the logs when you run the Flow,
flow.run()
and running the Flow are doing the same thing. 🤔
When you ran locally, did you do
flow.run()
or did you register and run with local agent?
m
I did
python flow.py
that has a register command in there, also added the
task.run()
before registering. I’m assuming that run in locally on my machine with no agent involved?
k
Yes that just calls the Python function underneath but before
task.run()
, how did you run it?
m
Just like I mentioned,
python flow.py
ran it for me. and it was successful.
I found the problem @Kevin Kho, Its the order of operations. basically, Prefect’s infer’s task execution order out of the order I’d want the task to be executed. So apparently, no issue w/Sqlalchemy connection. Can you share with me a few example where I could define the order and
task.set_dependancies()
maybe?
k
Yeah!
Copy code
@task()
def abc(a):
    return a
with Flow("ecs_testing") as flow:
    x = abc(1)
    y = abc(1, upstream_tasks=[x])
with Flow("ecs_tes") as flow2:
    x = abc(1)
    y = abc(1)
    y.set_upstream(x)