Class Default
- Namespace
- YndigoBlue.Velocity.Constraints
- Assembly
- YndigoBlue.Velocity.dll
Represents a default value constraint that automatically assigns a value to a column when no value is specified during insert.
public class Default : Constraint, IEquatable<Constraint>, IComparable<Constraint>
- Inheritance
-
Default
- Implements
Remarks
Default constraints provide automatic values for columns, such as timestamps, sequential numbers, or constant values. They are applied when INSERT statements don't provide explicit values for the column.
Examples
using (var manager = new Manager(connection))
{
var schema = manager.LoadSchema("myschema");
Table table = schema.CreateTable("orders");
table.CreateColumn("id", DataType.Integer, autoGenerate: true);
table.CreateColumn("status", DataType.VarChar, size: 20);
table.CreateColumn("created_at", DataType.DateTime);
table.CreateColumn("is_active", DataType.Boolean);
table.CreateColumn("priority", DataType.Integer);
// Default string value
Default defaultStatus = table.CreateDefaultConstraint("status",
new Literal<string>("Pending"), DefaultConstraintType.String, DataType.VarChar);
// Default to current timestamp using the Now() function
Default defaultCreatedAt = table.CreateDefaultConstraint("created_at",
new Now(), DefaultConstraintType.Function, DataType.DateTime);
// Default boolean value
Default defaultActive = table.CreateDefaultConstraint("is_active",
new Literal<bool>(true), DefaultConstraintType.Boolean, DataType.Boolean);
// Default numeric value
Default defaultPriority = table.CreateDefaultConstraint("priority",
new Literal<double>(1.0D), DefaultConstraintType.Number, DataType.Integer);
manager.CreateTable(table);
manager.CreateConstraint(defaultStatus);
manager.CreateConstraint(defaultCreatedAt);
manager.CreateConstraint(defaultActive);
manager.CreateConstraint(defaultPriority);
}
Properties
- Column
Gets the name of the column that this default constraint applies to.
- ConstraintType
Gets the constraint type as Default.
- DataType
Gets the data type of the column.
- DefaultConstraintType
Gets the type of default constraint (String, Number, DateTime, Date, Timestamp, Boolean, or Guid).
- DefaultItem
Gets the default value item (can be a literal value, function call, or expression).
Methods
- IsEqual(Constraint)
Determines whether this constraint is equal to another constraint based on their properties.