getcandy / candy-api

GetCandy v1 E-Commerce API
https://getcandy.io
Apache License 2.0
445 stars 115 forks source link

Error 500 when updating discount #398

Open geantas opened 3 years ago

geantas commented 3 years ago

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'
    })
  2. Get status 201
  3. Get the discount ID from server response (fx. wz6d39dj).
  4. 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'
              }
            ]
          }
        }
      ]
    }
    })
  5. See error in the server response.

Expected behavior Server returns a response with status 200.

johshej commented 3 years ago

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