microsoft / microsoft/DacFx

How get the default value if the specific field

Open
#400 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
460
Forks
29
Avg merge
4d 9h
Merged PRs (30d)
7

Description

I'm using t4 C3 script to create some SQL server object dynamically from predefined tables. Everything works except default values
Example
```
intake.TabA(
id int not null,
Name varchar(10) not null default('')
)
```
I need to generate persist table as follows
```
persist.TabA(
id int not null,
Name varchar(10) not null **default('')**
DateEff datetime,
DateExp datetime
)
```

I can generate everything but not **default('')**
Here is the snippet
```
foreach (var col in table.GetReferenced(Table.Columns))
{

string columnText;
string columnName = col.Name.Parts[2];
//string columnName3 = col.Name.Parts[3];
//string columnName4 = col.Name.Parts[4];

// this attempts to limit to only columns from the source. there's gotta be a cleaner way.
if (!skipColumns.Contains(columnName))
{

int length = col.GetProperty(Column.Length);
int precision = col.GetProperty(Column.Precision);
int scale = col.GetProperty(Column.Scale);
//string defaultExpression = col.GetProperty(Column.Expression).ToString();

string suffix;
if (length != 0)
{
suffix = String.Format("({0})", length);
}
else if (precision != 0)
{
suffix = String.Format("({0},{1})", precision, scale);
}
else if (precision == 0 && scale != 0)
{
suffix = String.Format("({0})", scale);
}
else
{
suffix = "";
}

bool nullable = col.GetProperty(Column.Nullable);
string nullText = nullable ? "NULL" : "NOT NULL";

string dataType = col.GetReferenced(Column.DataType).FirstOrDefault().Name.ToString();
//string defaultExpression = "";
//defaultExpression = col.GetReferenced(Column.Expression).FirstOrDefault().Name.ToString();
////string dataType = col.GetReferenced(Column.Con)

columnText = String.Format("[{0}] {1}{2} {3} {4}", columnName, dataType, suffix, nullText, defaultExpression);

WriteLine(" " + columnText + ",");
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the shown column-generation loop and investigate the Column.Expression metadata access, comparing GetProperty and GetReferenced for the default constraint. Confirm the generated persist.TabA definition preserves default('') alongside the column's type and nullability; no specific source file or test is identified in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.