You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SQL Server: Shim enums as table/key or as custom types with rules #6823
I currently can't create enums when working with the SqlServer provider, which means I have to design my schema differently and won't get any intellisense or type information during development when working with "enum" fields.
Suggested solution
I can think of a few options.
The creation and maintenance of a joined table containing the enum could be obfuscated and queries could just automatically join to replace the enum, though this may make problems for raw queries and anyone doing something manual with the database.
After thinking it over a bit though, perhaps the option with the least downsides would be creating the column with a CHECK parameter or as a custom type with an @list rule, and storing the enum key just as a string directly.
e.g.
CREATE RULE [dbo].[list_rule] AS @list IN ('USER', 'ADMIN');
GOCREATETYPE [dbo].[TestType] FROMVARCHAR(5) NOTNULLGOEXECsys.sp_bindrule @rulename=N'[dbo].[list_rule]', @objname=N'[dbo].[TestType]' , @futureonly='futureonly'GOCREATETABLEdbo.Table_1
(
/* You could create a reusable "Enum" by creating it as a type, or just adding the check constraint for a single one */
Test dbo.TestTypeNULL,
Test2 nchar(5) NOTNULLCONSTRAINT Test2_Enum CHECK (Test2 IN('USER', 'ADMIN')),
) ON [PRIMARY]
GO
Alternatives
At the moment, I'm considering creating a seed script to populate my tables that would otherwise be enums, but this doesn't provide intellisense during development and means I have to build my schema specially for MSSQL.
Reacted by evandeininger, Ige, skulliah, Jon Parton, mp205, Bruno Gomes, Ernesto Alejo, Ait Brahim Mohammed Khalil, Val Liu, Zack Taylor and 1 more
changed the title [-][Feature Request] SqlServer - shim enums as table/key or as custom types with rules[/-][+]SQL Server: Shim enums as table/key or as custom types with rules[/+]on Aug 8, 2022
Activity
JasonAtallah commentedon Aug 6, 2022
Are there any plans to move forward with this?
[-][Feature Request] SqlServer - shim enums as table/key or as custom types with rules[/-][+]SQL Server: Shim enums as table/key or as custom types with rules[/+]Katli95 commentedon Aug 21, 2022
I'm not qualified to comment on if this would be the best way, but I'd love having access to something like an enum with the SQL Server Provider!