schemazen icon indicating copy to clipboard operation
schemazen copied to clipboard

Default Constraints not generated when calling Table.ScriptCreate

Open aboy021 opened this issue 2 years ago • 0 comments

When I script the creation of a table that has a default value for a column it is not scripted.

I've written a crude test that recreates the issue on my machine:

        [Test]
        public void SchemaZen_whenCreatingTable_ScriptsDefaults()
        {
            var conn = ConnectionStringForNewEmptyDatabase();
            try
            {
                DBHelper.ExecSql(conn,
                                 @"
CREATE TABLE [dbo].[RoundTableSeat](
	[RoundTableSeatId] [bigint] IDENTITY(1,1) NOT NULL,
	[KnightID] [bigint] NOT NULL,
	[DateCreated] [datetime] NOT NULL CONSTRAINT [DF_RoundTableSeat_DateCreated]  DEFAULT (getdate())
	)");

                var db = new Database { Connection = conn };
                db.Load();
                var table = db.FindTable("RoundTableSeat", "dbo");
                var script = table.ScriptCreate();
                Console.WriteLine(script);
                Assert.That(script, Does.Contain("getdate()"));
            }
            finally
            {
                DropEmptyDatabase(conn);
            }
        }

This works correctly in 1.3.56 (0e0ad5262cfacc5c6b5f804bd0829e9ef3764f52) and comparing it to bacee1a768df0991995518954c10bd3250ffdef8 it looks like the difference comes from Column.cs. This section has changed:

	public string ScriptCreate() {
		return ScriptBase();
	}

	public string ScriptAlter() {
		return ScriptBase();
	}

Previously, ScriptBase took a boolean argument includeDefaultConstraint which helped determine if the default values should be scripted inline, but this is now determined via the IncludeDefaultConstraint property.

if (IncludeDefaultConstraint) val.Append(DefaultText);

I'm not totally sure how best to fix this one, since I don't fully understand the properties involved. I suspect it's related to the fact that Default.cs now knows about the table its in, though looking at how it's used this could be a bug too.

aboy021 avatar May 06 '22 20:05 aboy021