Model.new with wrong default value
I'm using activerecord-sqlserver-adapter (4.2.15) with rails 4.2.7. My migrations look like this: create_table :users, id: false do |t| t.string :id, null: false t.string :name t.timestamps end I use string ids, as I try to use my rails database with azure app service. The app service requires the id to be of type string.
Sth. like this:
create_table :users, id: :uuid
won't work as my id columns would be of type uniqueidentifier.
Moreover I execute a SQL statement in my migrations to set a default value if none is given and make the id primary key: execute "ALTER TABLE users ADD CONSTRAINT [DF_users_id] DEFAULT LOWER((CONVERT(nvarchar,newid(),(0)))) FOR [id]" execute "ALTER TABLE users ADD PRIMARY KEY NONCLUSTERED ([id] ASC);"
, because an additional requirement for app service is to create an uuid if none is given.
So and now my problem.
When calling User.new in rails the id of the User object is set to an non nil value sth. like "2d931510-d99f-494a-8c67-87feb05e1594". However my rails app counts on having a nil value for new User objects. Moreover when I call User.new multiple times it always has exactly the same id value. Restarting the server changes this default id value.
I tried to use create_table :users, id: :uuid as mentioned above. When I now call User.new the default value for id is nil as it should be. So I think it should be possible to get nil id values for new objects, however I don't know if I did sth. wrong or if I found a bug.
Sorry for the late reply.
create_table :users, id: :uuid won't work as my id columns would be of type uniqueidentifier.
What is wrong with that? That type uses newid() function as the default and that seems to be exactly what you want?
Closing this old issue, but please reply as needed.