PostgreSQL · Update
Change prices found by a compound key
The price list has no id column; each price is found by its region and SKU. Three scenarios: change one price by the compound key; move a price to another SKU (changing part of the key, found by the old values through columns); rename a natural id that other rows still use, which the foreign key refuses.
Set up for this page
The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleCatalog.cs (the tables' ids, keys, lookups and display names).
Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:
var norway = new ArxEntity("country");
norway["code"] = "NO";
norway["name"] = "Norway";
await service.CreateAsync(norway);
var prices = new[] { "SKU-U501", "SKU-U502" }.Select(sku =>
{
var price = new ArxEntity("price_list");
price["region"] = "NO";
price["sku"] = sku;
price["unit_price"] = 20.00m;
price["valid_from"] = new DateOnly(2026, 1, 1);
return price;
}).ToList();
await service.CreateMultipleAsync(prices);1 One price, by region and SKU
Region and SKU together find the price: ArxMatch.Key with both columns.
Already in scope: service (ArxPgDbService)
var price = new ArxEntity("price_list");
price["region"] = "NO";
price["sku"] = "SKU-U501";
price["unit_price"] = 24.90m;
await service.UpdateAsync(price, new ArxUpdateOptions { Match = ArxMatch.Key("region", "sku") });
Console.WriteLine("NO / SKU-U501 is now 24.90");Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT sku, unit_price
FROM price_list
WHERE region = 'NO' AND sku = 'SKU-U501';Expect: 1 row, 24.90.
2 Renumber a SKU: find the row by other columns
The SKU itself changes (the product was renumbered). The key columns can't both find the row and be written, so find it by other columns: here the region and its current price, with RequireSingleRow so a second match refuses it.
Already in scope: service (ArxPgDbService)
var move = new ArxEntity("price_list");
move["region"] = "NO";
move["unit_price"] = 20.00m;
move["sku"] = "SKU-U502-NEW";
await service.UpdateAsync(move, new ArxUpdateOptions { Match = ArxMatch.Columns("region", "unit_price"), RequireSingleRow = true });
Console.WriteLine("SKU-U502 renumbered to SKU-U502-NEW");Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT sku, unit_price
FROM price_list
WHERE region = 'NO'
ORDER BY sku;Expect: SKU-U501 24.90, SKU-U502-NEW 20.00.
3 Rename a natural id that prices still use
Renaming the country code NO would orphan its prices: the foreign key refuses it (ARX3002).
Already in scope: service (ArxPgDbService)
var rename = new ArxEntity("country");
rename["name"] = "Norway";
rename["code"] = "NW";
try
{
await service.UpdateAsync(rename, new ArxUpdateOptions { Match = ArxMatch.Columns("name") });
}
catch (ArxDatabaseException error) when (error.Code == ArxErrorCode.ForeignKeyViolation)
{
Console.WriteLine(error.Message);
}Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT code, name
FROM country
WHERE name = 'Norway';Expect: 1 row, NO: unchanged.
Run the complete sample
The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.
The program, its files and how to run it
Run it locally
- .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target
net10.0). - Create the project:
Terminal dotnet new console -n ArxSample -f net10.0 cd ArxSample - Install the packages:
Terminal dotnet add package ARX.SDK.PostgreSql --version 1.0.0-rc.1 - Database: a PostgreSQL 16 (tested with 16.15) database with the sample schema.
- Configuration: set
ARX_SAMPLES_POSTGRESQLto its connection string. Nothing is hard-coded. - Source code: add the files of the sample application this sample uses (
SampleContext.cs,SampleOutput.cs,SampleContext.Application.cs,SampleCatalog.cs), then replaceProgram.csand add the other file below, with the file names shown. - Run it:
Terminal dotnet run - Expected output: below.
The files
Runs the sample. Generated for this page; it reads the connection string from the environment.
using ArxSamples.PostgreSql;
using ArxSamples.PostgreSql.Operations.Update.RealCases;
// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_POSTGRESQL")
?? throw new InvalidOperationException("Set the ARX_SAMPLES_POSTGRESQL environment variable to your PostgreSQL connection string.");
await new RealCase3_ChangeAPriceListEntry().RunAsync(new SampleContext(connectionString));
The sample, as it is in the repository.
using ARX.SDK;
using ARX.SDK.Services;
using static ArxSamples.PostgreSql.SampleOutput;
namespace ArxSamples.PostgreSql.Operations.Update.RealCases;
/// <summary>
/// The price list has no id column; each price is found by its region and SKU. Three scenarios: change one price by
/// the compound key; move a price to another SKU (changing part of the key, found by the old values through columns);
/// rename a natural id that other rows still use, which the foreign key refuses.
/// </summary>
public sealed class RealCase3_ChangeAPriceListEntry : ISample
{
public string Name => "update-real-3-change-a-price-list-entry";
public string Title => "Change prices found by a compound key";
public async Task RunAsync(SampleContext dbContext)
{
var norway = new ArxEntity("country");
norway["code"] = "NO";
norway["name"] = "Norway";
await dbContext.Service.CreateAsync(norway);
var prices = new[] { "SKU-U501", "SKU-U502" }.Select(sku =>
{
var price = new ArxEntity("price_list");
price["region"] = "NO";
price["sku"] = sku;
price["unit_price"] = 20.00m;
price["valid_from"] = new DateOnly(2026, 1, 1);
return price;
}).ToList();
await dbContext.Service.CreateMultipleAsync(prices);
await Scenario1_OnePriceByItsKeyAsync(dbContext);
await Scenario2_MoveAPriceToAnotherSkuAsync(dbContext);
await Scenario3_RenameANaturalIdInUseAsync(dbContext);
}
/// <summary>1. Region and SKU together find the price: ArxMatch.Key with both columns.</summary>
private static async Task Scenario1_OnePriceByItsKeyAsync(SampleContext dbContext)
{
Step("1. One price, by region and SKU");
var price = new ArxEntity("price_list");
price["region"] = "NO";
price["sku"] = "SKU-U501";
price["unit_price"] = 24.90m;
await dbContext.Service.UpdateAsync(price, new ArxUpdateOptions { Match = ArxMatch.Key("region", "sku") });
Show("NO / SKU-U501 is now 24.90");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT sku, unit_price
* FROM price_list
* WHERE region = 'NO' AND sku = 'SKU-U501';
* -- Expect: 1 row, 24.90.
* ===========================================================
*/
}
/// <summary>
/// 2. The SKU itself changes (the product was renumbered). The key columns can't both find the row and be written, so
/// find it by other columns: here the region and its current price, with RequireSingleRow so a second match refuses it.
/// </summary>
private static async Task Scenario2_MoveAPriceToAnotherSkuAsync(SampleContext dbContext)
{
Step("2. Renumber a SKU: find the row by other columns");
var move = new ArxEntity("price_list");
move["region"] = "NO";
move["unit_price"] = 20.00m;
move["sku"] = "SKU-U502-NEW";
await dbContext.Service.UpdateAsync(move, new ArxUpdateOptions { Match = ArxMatch.Columns("region", "unit_price"), RequireSingleRow = true });
Show("SKU-U502 renumbered to SKU-U502-NEW");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT sku, unit_price
* FROM price_list
* WHERE region = 'NO'
* ORDER BY sku;
* -- Expect: SKU-U501 24.90, SKU-U502-NEW 20.00.
* ===========================================================
*/
}
/// <summary>3. Renaming the country code NO would orphan its prices: the foreign key refuses it (ARX3002).</summary>
private static async Task Scenario3_RenameANaturalIdInUseAsync(SampleContext dbContext)
{
Step("3. Rename a natural id that prices still use");
var rename = new ArxEntity("country");
rename["name"] = "Norway";
rename["code"] = "NW";
try
{
await dbContext.Service.UpdateAsync(rename, new ArxUpdateOptions { Match = ArxMatch.Columns("name") });
}
catch (ArxDatabaseException error) when (error.Code == ArxErrorCode.ForeignKeyViolation)
{
ShowError(error);
}
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT code, name
* FROM country
* WHERE name = 'Norway';
* -- Expect: 1 row, NO: unchanged.
* ===========================================================
*/
}
}
Expected output
What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.
> 1. One price, by region and SKU
NO / SKU-U501 is now 24.90
> 2. Renumber a SKU: find the row by other columns
SKU-U502 renumbered to SKU-U502-NEW
> 3. Rename a natural id that prices still use
caught ArxDatabaseException: ARX3002 ForeignKeyViolation: Update on 'country' was refused: rows of 'price_list' still reference it (foreign key constraint 'price_list_region_fkey').