Yup, overriding SqlType was the only way I could get it working. Otherwise, the base EnumStringType class generates SQL statements with the wrong cast. Something like:
INSERT INTO TestTable (State, Id) VALUES ('Preview'::text, '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) Which creates an error, since text can't be casted to the DB enum. On Sun, Jun 6, 2010 at 9:02 PM, Mauricio Scheffer <mauricioschef...@gmail.com> wrote: > Yep, that's pretty much what I answered on stackoverflow ( > http://stackoverflow.com/questions/2979026/mapping-enums-to-database-with-nhibernate-castle-activerecord > , I just see it here). Is it really necessary to override the SqlType > property? > > On Jun 5, 3:09 am, Mike Christensen <m...@kitchenpc.com> wrote: >> Update: >> >> Ok I found a kinda cleverish/hacky way of doing this. First off, I >> only have one mapping class: >> >> public class PgEnumMapper<T> : NHibernate.Type.EnumStringType<T> >> { >> public override NHibernate.SqlTypes.SqlType SqlType >> { >> get { return new NHibernate.SqlTypes.SqlType(DbType.Object); } >> } >> >> } >> >> Then on my property, I setup the mapping like so: >> >> [Property(NotNull = true, SqlType = "OrderState", ColumnType = >> "DB.PgEnumMapper`1[DB.OrderState, WebSite], WebSite")] >> public OrderState State >> { >> get { return state; } >> set { state = value; } >> >> } >> >> If I find nothing better, I'll go this route.. I don't like it 100% >> because it looks kinda hacky, and also if I change enum names, Visual >> Studio cannot refactor them since they're all in strings.. Am I going >> down the right path, or is there some stupidly easy approach I'm >> missing? Thanks! >> >> Mike >> >> On Jun 4, 10:47 pm, Mike Christensen <m...@kitchenpc.com> wrote: >> >> >> >> > By default, an enum property gets mapped to an integer column type in >> > the database. I'm interested in mapping enums to an enum database >> > type, such as this one: >> >> > CREATE TYPE OrderState AS ENUM ('Preview', 'InQueue', 'Ordered', >> > 'Error', 'Cancelled'); >> >> > It seems you can fairly easily change ActiveRecord to use strings for >> > enums instead of integers, and there's several blog posts I've found >> > on this. However, they have one big flaw. The SQL that gets >> > generated looks something like this: >> >> > INSERT INTO TestTable (State, Id) VALUES ('Preview'::text, >> > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) >> >> > As you can see, the State value is casted to a text, which will throw >> > a SQL error as the column type is OrderState. I've found one way to >> > fix this, but I don't like my solution. I basically create an IType >> > that knows how to map this enum: >> >> > public class OrderStateMapper : >> > NHibernate.Type.EnumStringType<OrderState> >> > { >> > public override NHibernate.SqlTypes.SqlType SqlType >> > { >> > get { return new NHibernate.SqlTypes.SqlType(DbType.Object); } >> > } >> >> > } >> >> > Since I override SqlType and return DbType.Object, NHibernate will no >> > longer try to cast my value into a text and everything will work. The >> > reason I don't like this solution is I now have to create a Mapper >> > class for every enum I want to map to my database, which in my case is >> > a lot and I want to be able to easily add more and not worry about >> > this issue. I can see why EnumStringType needs to know the type of >> > the enum, so it can parse data values and rehydrate OrderState >> > objects. However, isn't there a way to tell ActiveRecord to just use >> > the string value for the enum and not try to cast it to text? >> > Basically the SQL statement I want to generate is: >> >> > INSERT INTO TestTable (State, Id) VALUES ('Preview', >> > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) >> > or even >> > INSERT INTO TestTable (State, Id) VALUES ('Preview'::OrderState, >> > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) >> >> > Thanks!! >> >> > Mike > > -- > You received this message because you are subscribed to the Google Groups > "Castle Project Users" group. > To post to this group, send email to castle-project-us...@googlegroups.com. > To unsubscribe from this group, send email to > castle-project-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/castle-project-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to castle-project-us...@googlegroups.com. To unsubscribe from this group, send email to castle-project-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.