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

Mark the Order.OrderGuid field as unique in the database #7377

Closed
skoshelev opened this issue Oct 23, 2024 · 3 comments
Closed

Mark the Order.OrderGuid field as unique in the database #7377

skoshelev opened this issue Oct 23, 2024 · 3 comments

Comments

@skoshelev
Copy link
Contributor

At the moment, it is only implied that the Order.OrderGuid field will be unique for a specific database, but in reality, there are no corresponding checks, which can lead to collisions, for example, when importing data from external sources

@skoshelev
Copy link
Contributor Author

Closed #7377

@Hakkology
Copy link

If you are just making a nopcommerce site, this gives an error as constraint is already available.
Perhaps another boolean check could be considered ?

        if (!Schema.Table(nameof(Order)).Index("AK_Order_OrderGuid").Exists()
            && !Schema.Table(nameof(Order)).Constraint("AK_Order_OrderGuid").Exists())
        {
            var orders = _dataProvider.GetTable<Order>().GroupBy(p => p.OrderGuid, p => p)
                .Where(p => p.Count() > 1)
                .SelectMany(p => p)
                .ToList();

            if (orders.Any())
            {
                foreach (var order in orders)
                    order.OrderGuid = Guid.NewGuid();

                _dataProvider.UpdateEntities(orders);
            }

            Create.UniqueConstraint("AK_Order_OrderGuid")
                .OnTable(nameof(Order))
                .Column(nameof(Order.OrderGuid));
        }

skoshelev added a commit that referenced this issue Dec 12, 2024
@skoshelev
Copy link
Contributor Author

Hi @Hakkology.

Thank you for your help

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

No branches or pull requests

3 participants