Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Constraints not generated when calling Table.ScriptCreate #207

Open
aboy021 opened this issue May 6, 2022 · 0 comments
Open

Default Constraints not generated when calling Table.ScriptCreate #207

aboy021 opened this issue May 6, 2022 · 0 comments

Comments

@aboy021
Copy link

aboy021 commented May 6, 2022

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 (0e0ad52) and comparing it to bacee1a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant