0

I have two tables in my project call task and params, task represents work task and params are the param list of that task. they are one2many relationship. params has a column called task_id which contains the id of the task table(foreign key).

Here I insert a row into the task table and then insert some rows into params table:

1.task.create(vals)
2.params.create(param_vals)

But due to the transaction when the 2 line is executed the task was not inserted so the task_id was not generated. How to avoid this?

Don_Chen
  • 987
  • 2
  • 8
  • 16

1 Answers1

0

This is not how a SQL transaction works.

If you have a transaction in 2 steps like this:

  • step 1 insert in task

  • step 2 insert in params

Then the task ID will be available inside the transaction. So when you insert in tasks you need to read the PK ID and then use it in params.

Something like this: Python/postgres/psycopg2: getting ID of row just inserted

Community
  • 1
  • 1
Victorqedu
  • 484
  • 4
  • 20