Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Duplicate Items in a Recipe cause duplicate key exception #615

Open
celaus opened this issue Dec 29, 2024 · 0 comments
Open

Bug: Duplicate Items in a Recipe cause duplicate key exception #615

celaus opened this issue Dec 29, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@celaus
Copy link

celaus commented Dec 29, 2024

Is this urgent?

No

What parts are affected

Backend

What is the server version

0.6.7

What is the client version

none

What platform are you using

Linux/Docker + Postgres

What's the problem 🤔

Inserting a recipe via POST to /api/household/1/recipe as follows results in a "Something went wrong" response. Note the duplicate "Water" item. The recipe itself is still inserted it just lacks the full list of ingredients, so that's another bug (I'll open a separate issue). Inserts are done with a Python script.

{
  "name": "Ketchup",
  "description": "Mix together in a pot (everything except clove)\nAdd the clove once boiling, leave in for about 2 minutes\nMix and add consistency patch\nEnough for about 1 week",
  "time": 0.0,
  "cook_time": 0,
  "prep_time": 0,
  "yields": 0.0,
  "source": "",
  "public": false,
  "tags": [],
  "items": [
    {
      "name": "tomato paste",
      "description": "4.0 tbsp",
      "optional": false
    },
    {
      "name": "vinegar",
      "description": "6.0 tbsp",
      "optional": false
    },
    {
      "name": "water",
      "description": "6.0 tbsp",
      "optional": false
    },
    {
      "name": "sugar",
      "description": "2.0 tbsp",
      "optional": false
    },
    {
      "name": "salt",
      "description": "0.5 tbsp",
      "optional": false
    },
    {
      "name": "starch",
      "description": "0.5 tbsp",
      "optional": false
    },
    {
      "name": "water",
      "description": "0.5 tbsp",
      "optional": false
    }
  ]
}

Share your logs

kitchenowl-back  [SQL: INSERT INTO recipe_items (recipe_id, item_id, description, optional, created_at, updated_at) VALUES (%(recipe_id)s, %(item_id)s, %(description)s, %(optional)s, %(created_at)s, %(updated_at)s)]
 kitchenowl-back  [parameters: {'recipe_id': 106, 'item_id': 626, 'description': '0.5 tbsp', 'optional': False, 'created_at': datetime.datetime(2024, 12, 29, 20, 56, 23, 579266, tzinfo=datetime.timezone.utc), 'updated_at': datetime.datetime(2024, 12, 29, 20, 56, 23, 579286, tzinfo=datetime.timezone.utc)}]
 kitchenowl-back  (Background on this error at: https://sqlalche.me/e/20/gkpj)
 kitchenowl-back  Traceback (most recent call last):
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
 kitchenowl-back      self.dialect.do_execute(
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 941, in do_execute
 kitchenowl-back      cursor.execute(statement, parameters)
 kitchenowl-back  psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "pk_recipe_items"
 kitchenowl-back  DETAIL:  Key (recipe_id, item_id)=(106, 626) already exists.
 kitchenowl-back  
 kitchenowl-back  
 kitchenowl-back  The above exception was the direct cause of the following exception:
 kitchenowl-back  
 kitchenowl-back  Traceback (most recent call last):
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request
 kitchenowl-back      rv = self.dispatch_request()
 kitchenowl-back           ^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request
 kitchenowl-back      return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/flask_jwt_extended/view_decorators.py", line 170, in decorator
 kitchenowl-back      return current_app.ensure_sync(fn)(*args, **kwargs)
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/usr/src/kitchenowl/app/helpers/authorize_household.py", line 26, in decorator
 kitchenowl-back      return func(*args, **kwargs)  # case server admin
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/usr/src/kitchenowl/app/helpers/validate_args.py", line 27, in func_wrapper
 kitchenowl-back      return func(arguments, *args, **kwargs)
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/usr/src/kitchenowl/app/controller/recipe/recipe_controller.py", line 75, in addRecipe
 kitchenowl-back      con.save()
 kitchenowl-back    File "/usr/src/kitchenowl/app/helpers/db_model_mixin.py", line 17, in save
 kitchenowl-back      raise e
 kitchenowl-back    File "/usr/src/kitchenowl/app/helpers/db_model_mixin.py", line 14, in save
 kitchenowl-back      db.session.commit()
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/scoping.py", line 597, in commit
 kitchenowl-back      return self._proxied.commit()
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2028, in commit
 kitchenowl-back      trans.commit(_to_root=True)
 kitchenowl-back    File "<string>", line 2, in commit
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py", line 139, in _go
 kitchenowl-back      ret_value = fn(self, *arg, **kw)
 kitchenowl-back                  ^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 1313, in commit
 kitchenowl-back      self._prepare_impl()
 kitchenowl-back    File "<string>", line 2, in _prepare_impl
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py", line 139, in _go
 kitchenowl-back      ret_value = fn(self, *arg, **kw)
 kitchenowl-back                  ^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 1288, in _prepare_impl
 kitchenowl-back      self.session.flush()
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4352, in flush
 kitchenowl-back      self._flush(objects)
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4487, in _flush
 kitchenowl-back      with util.safe_reraise():
 kitchenowl-back           ^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
 kitchenowl-back      raise exc_value.with_traceback(exc_tb)
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4448, in _flush
 kitchenowl-back      flush_context.execute()
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py", line 466, in execute
 kitchenowl-back      rec.execute(self)
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/unitofwork.py", line 642, in execute
 kitchenowl-back      util.preloaded.orm_persistence.save_obj(
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 93, in save_obj
 kitchenowl-back      _emit_insert_statements(
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 1048, in _emit_insert_statements
 kitchenowl-back      result = connection.execute(
 kitchenowl-back               ^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
 kitchenowl-back      return meth(
 kitchenowl-back             ^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 515, in _execute_on_connection
 kitchenowl-back      return connection._execute_clauseelement(
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1640, in _execute_clauseelement
 kitchenowl-back      ret = self._execute_context(
 kitchenowl-back            ^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
 kitchenowl-back      return self._exec_single_context(
 kitchenowl-back             ^^^^^^^^^^^^^^^^^^^^^^^^^^
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
 kitchenowl-back      self._handle_dbapi_exception(
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2355, in _handle_dbapi_exception
 kitchenowl-back      raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
 kitchenowl-back      self.dialect.do_execute(
 kitchenowl-back    File "/opt/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 941, in do_execute
 kitchenowl-back      cursor.execute(statement, parameters)
 kitchenowl-back  sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "pk_recipe_items"
 kitchenowl-back  DETAIL:  Key (recipe_id, item_id)=(106, 626) already exists.
 kitchenowl-back  
 kitchenowl-back  [SQL: INSERT INTO recipe_items (recipe_id, item_id, description, optional, created_at, updated_at) VALUES (%(recipe_id)s, %(item_id)s, %(description)s, %(optional)s, %(created_at)s, %(updated_at)s)]
 kitchenowl-back  [parameters: {'recipe_id': 106, 'item_id': 626, 'description': '0.5 tbsp', 'optional': False, 'created_at': datetime.datetime(2024, 12, 29, 20, 56, 23, 579266, tzinfo=datetime.timezone.utc), 'updated_at': datetime.datetime(2024, 12, 29, 20, 56, 23, 579286, tzinfo=datetime.timezone.utc)}]
 kitchenowl-back  (Background on this error at: https://sqlalche.me/e/20/gkpj)

Share your configuration

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Next Up
Development

No branches or pull requests

1 participant