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

Error 500 when updating discount #398

Open
geantas opened this issue Jun 18, 2021 · 1 comment
Open

Error 500 when updating discount #398

geantas opened this issue Jun 18, 2021 · 1 comment

Comments

@geantas
Copy link

geantas commented Jun 18, 2021

Describe the bug
When updating a discount through an API, it throws an error related to SQL:

"SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`homestead`.`discount_reward_products`, CONSTRAINT `discount_reward_products_discount_reward_id_foreign` FOREIGN KEY (`discount_reward_id`) REFERENCES `discount_rewards` (`id`)) (SQL: delete from `discount_rewards` where `discount_rewards`.`discount_id` = 5 and `discount_rewards`.`discount_id` is not null)"

To Reproduce

  1. Create a discount, fx:
this.$getcandy.on('discounts', 'postDiscounts',
   {
     name: {
       en: 'Buy one get none free',
       da: 'Buy one get none free'
     },
     start_at: '2019-06-04T14:27:02.000Z',
     end_at: '2019-06-04T14:27:02.000Z'
   })
  1. Get status 201
  2. Get the discount ID from server response (fx. wz6d39dj).
  3. Update discount, fx:
this.$getcandy.on('discounts', 'putDiscountsDiscountId', 'wz6d39dj', {
    start_at: '2019-08-24T14:15:22Z',
    end_at: '2022-08-24T14:15:22Z',
    priority: 0,
    stop_rules: true,
    status: true,
    attribute_data: {
      name: {
        webstore: {
          da: 'Buy one get none free',
          en: 'Buy one get none free'
        }
      }
    },
    channels: {
      data: {
        id: 1,
        published_at: '2021-06-14T14:15:22Z'
      }
    },
    rewards: {
      data: {
        products: [
          {
            product_id: 24202,
            quantity: 1
          }
        ]
      }
    },
    sets: {
      data: [
        {
          scope: 'string',
          outcome: true,
          items: {
            data: [
              {
                eligibles: [
                  'string'
                ],
                type: 'string'
              }
            ]
          }
        }
      ]
    }
  })
  1. See error in the server response.

Expected behavior
Server returns a response with status 200.

@johshej
Copy link

johshej commented Jun 21, 2021

Is the table definition missing an "ON DELETE CASCADE" for the "discount_reward_products_discount_reward_id_foreign" foreign key?

CREATE TABLE `discount_reward_products` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int unsigned NOT NULL,
  `discount_reward_id` int unsigned NOT NULL,
  `quantity` int NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `discount_reward_products_discount_reward_id_foreign` (`discount_reward_id`),
  KEY `discount_reward_products_product_id_foreign` (`product_id`),
  CONSTRAINT `discount_reward_products_discount_reward_id_foreign` FOREIGN KEY (`discount_reward_id`) REFERENCES `discount_rewards` (`id`),
  CONSTRAINT `discount_reward_products_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants