Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set column default to SQL Server's NewId()?

In fluentMigrator, is it possible to set a column default to a function?

Specifically, I'm adding a uniqueidentifier (Guid) column to a SQL Server 2008 table that already contains data, and it's going to be a required field - is it possible to set the default to the NewId() function?

like image 340
Remi Despres-Smyth Avatar asked Sep 23 '11 14:09

Remi Despres-Smyth


People also ask

How do I change the default value of a column in postgresql?

Changing a Column's Default Value. To set a new default for a column, use a command like: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77; Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.

What is a default value in SQL?

The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new records, if no other value is specified.


1 Answers

Field defaults for NewId() and GetDate() are available (for SQL Server at least) by referring to either SystemMethods.NewGuid or SystemMethods.CurrentDateTime, for example:

Create
    .Column("incomeIdGuid").OnTable("tblIncome")
    .AsGuid()
    .NotNullable()
    .WithDefaultValue(SystemMethods.NewGuid);
like image 124
Remi Despres-Smyth Avatar answered Oct 12 '22 11:10

Remi Despres-Smyth