🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Attribute or Data Type Conversion
This page was created by Charles on 2024-07-14. Last edited by Wikiadmin on 2026-07-29.

You can change how an existing attribute is stored or presented by using a staged conversion strategy; this page is for modelers changing an attribute's data type without losing existing data.

Changing a persisted attribute type can change the database schema during database evolve. MDriven does not make every conversion automatically because a conversion can lose, truncate, or reinterpret data. Plan the conversion, back up the database, and verify the result before you remove or rename the original value.

Choose a conversion strategy

Use the strategy that matches whether you need a permanent storage change, explicit data transformation, or a safe trial.

Goal Recommended approach What happens to existing data
Change the stored type of one attribute and let evolve transfer the old column value Rename the attribute with FormerNames Evolve attempts to transform and transfer the old value to the new attribute.
Apply explicit logic, combine values, or calculate a replacement value Create a new attribute and populate it with an EAL method The old attribute remains available until you decide to retire it.
Test a presentation or calculation without changing stored data Create a derived attribute with OCL The derived value is calculated; deleting it does not delete persisted data.
Accept user input in one form while storing it in another Use a derived settable attribute The OCL expression presents the value and EAL writes the entered value back to source attributes.

Example: a Product.Price attribute currently stores an Integer, but prices now need decimal places. You can first expose and validate a Decimal representation with a derived attribute. When the conversion rules are known, either populate a new persisted Decimal attribute with a method or replace the stored attribute through the FormerNames workflow.

Before you change a type

  1. Back up the database before evolving the model.
  2. Identify values that may not fit the target type. For example, changing a numeric value to text changes how it is handled in expressions, while changing a Decimal's precision or scale can affect which values fit.
  3. Decide what should happen to values that cannot be represented by the new type. Do not assume database evolve can resolve this business rule.
  4. Test the conversion on representative data, including empty values and boundary values.
  5. Upload the model and review the result of database evolve. Some changes are not evolved automatically and must be handled manually in the database server.
  6. Verify converted values before removing the original attribute or making a rename permanent.

For Decimal storage settings such as precision and scale, see Documentation:Decimal. For type names and persistence mapping behavior, see Documentation:Type mapping, OR-Mapping.

Permanently change a stored attribute with FormerNames

Use FormerNames when you want the new persisted attribute to take over the previous attribute's database identity. This is appropriate when one old attribute maps to one replacement attribute and the evolve engine can perform the required transfer.

Procedure

  1. Select the existing attribute and change its type to the target type.
  2. Rename the attribute to a temporary new name and select Set Former Name when renaming it. This records the prior attribute name in the attribute's database property FormerNames.
  3. Upload the model so that database evolve can create the replacement attribute and attempt to transfer transformed data from the old attribute.
  4. Inspect the resulting values and confirm that the transfer produced the expected results.
  5. Rename the attribute back to the name required by the model and upload the model again.

The temporary rename is important: the old attribute is otherwise dropped or destroyed when the new attribute is created. FormerNames gives the evolver the previous column identity to use during the transfer.

Example: Integer price to Decimal price

Assume Product.aPrice is an Integer and the intended result is a Decimal price.

  1. Change aPrice to Decimal.
  2. Rename aPrice to a temporary name, such as aPriceDecimal, and select Set Former Name.
  3. Upload the model and verify that existing product prices have been transferred correctly.
  4. Rename aPriceDecimal back to aPrice and upload again.

If the required change includes rules beyond a direct type conversion—for example, adding a fixed amount or combining two old values—use a method instead.

Populate a new attribute with a method

Use a method when you need to control the transformation explicitly. This approach keeps the original persisted attribute available while you calculate and verify the replacement. It is also the appropriate approach when the new value depends on more than one attribute.

Procedure

  1. Create a new persisted attribute with the target type. For example, add xPrice as Decimal while retaining the existing Integer aPrice.
  2. Add a method to the class that performs the transformation.
  3. In the method body, write the transformation in EAL. Define the rule explicitly—for example, convert the old price and apply the required adjustment.
  4. While the Turnkey and MDrivenServer applications are running, open the debugger.
  5. Run the method in Action mode for the instances that require conversion.
  6. Save after executing the expression so the calculated values are persisted.
  7. Verify the new attribute values in the affected instances.
  8. Update ViewModels to use the new attribute, or use the FormerNames workflow to retire and rename attributes while preserving the name already used by ViewModels.

Example: convert and adjust a price

A product has aPrice as Integer. Add xPrice as Decimal and use a method to set xPrice from aPrice according to the business rule—for example, convert the value and increase it by 1000. Run the method for the intended Product instances, save, and verify the results before replacing the old ViewModel-facing name.

This method is safer than changing the original attribute first because you can compare aPrice and xPrice while both values exist.

Test a conversion with a derived attribute

A derived attribute is an attribute calculated from an OCL expression rather than stored as a database value. Use one to test a new type or transformation without modifying the source data.

  1. Add a derived attribute with the target type.
  2. Define its OCL expression using the existing attribute or attributes.
  3. Use the derived attribute in the relevant ViewModel and verify its values.
  4. Keep both attributes while evaluating the result, or delete the derived attribute to return to the original persisted attribute.

Deleting a derived attribute does not affect data in the database because the derived value is calculated rather than persisted.

Example: trial Decimal price

Keep aPrice as the stored Integer. Add a derived Decimal attribute, such as CalculatedPrice, that represents the intended price conversion and adjustment. Show CalculatedPrice in a ViewModel and compare it with aPrice. Once the rule is accepted, implement it as a persisted conversion with a method or FormerNames.

Derived attributes are also suitable for complex calculated transformations. If users must enter the converted representation and MDriven must update stored source values, use a derived settable attribute instead.

Conversion in expressions is not a storage conversion

An expression-level conversion changes the result of that expression; it does not change the attribute's stored type or database column. For example, asString produces a string representation of a value but does not convert the underlying attribute to String.

Use expression conversions for display and calculations. Use one of the persisted conversion strategies on this page when the stored attribute type must change.

SafeCast

SafeCast is listed as a conversion option, but this page does not define its syntax, supported source and target types, or persistence behavior. Do not use it to plan a database conversion until those details have been confirmed.

See also

[[Category:Attributes and data types [Beginner]]]