rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 558 forks source link

Primary Key not being stored #518

Closed PAMulligan closed 7 years ago

PAMulligan commented 7 years ago

I'm connecting to a legacy database with a Rails API that I'm writing. Currently trying to use FactoryGirl to create models on my test database.

The primary key for the User table is called 'UserId'. In the model, I've set

self.primary_key = 'UserId'

However, when I try this code:

user = FactoryGirl.create :user 

I get "TinyTds::Error Cannot insert null value into column 'UserId'". Any thoughts on why this may be happening?

metaskills commented 7 years ago

I think the same would be true if you consoled into your app and did the model create! right?

PAMulligan commented 7 years ago

Yes, that's accurate

metaskills commented 7 years ago

OK, so is this model a view? Have you made sure that your ID is auto-incrementing at the DB layer?

PAMulligan commented 7 years ago

It's not a view, this is for an API. I know this isn't necessarily an issue with the adapter, but I thought I'd start with this forum. Honestly, I'm used to dealing with Devise and pg, and the way I set things up, there is always an ID available. For this project, I'm not using Devise, and I'm connecting to a pre-existing SQL Server database, so I'm not sure that the ID is auto-incrementing.

On Wed, Oct 12, 2016 at 8:10 AM, Ken Collins notifications@github.com wrote:

OK, so is this model a view? Have you made sure that your ID is auto-incrementing at the DB layer?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/518#issuecomment-253196349, or mute the thread https://github.com/notifications/unsubscribe-auth/AMyos5W01aPBplg--x8gJvH8K2MYgoraks5qzM5TgaJpZM4KTI4u .

metaskills commented 7 years ago

I'm not sure that the ID is auto-incrementing

Ahh... I bet that is the issue. You can tell if you looked at the output of YourModel.columns_hash['UserId'] I bet it does not have the right info. Maybe reach out to the DB team to see if they can add that. If you have any amount of activity on this table, that would be the way to go.

PAMulligan commented 7 years ago

There is no team, it's just me and my boss. He wrote our initial code a few years ago, so I suppose I'll try asking him. I do know the primary key in the schema for that model is listed as "UserId", which is why I thought changing the primary key property in the model would do the trick. Thank you for the help, I'll see if I can muddle through!

On Wed, Oct 12, 2016 at 10:03 AM, Ken Collins notifications@github.com wrote:

I'm not sure that the ID is auto-incrementing

Ahh... I bet that is the issue. You can tell if you looked at the output of YourModel.columns_hash['UserId'] I bet it does not have the right info. Maybe reach out to the DB team to see if they can add that. If you have any amount of activity on this table, that would be the way to go.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/518#issuecomment-253222220, or mute the thread https://github.com/notifications/unsubscribe-auth/AMyoswyA288HZhJyA1vL0G4SrxZxLY9hks5qzOiigaJpZM4KTI4u .

PAMulligan commented 7 years ago

I'm still lost on this, the output of User.columns_hash['UserId'] is indeed nil. I know this problem is due to my ignorance, but I have to deliver something usable this week, and I'm about to just create an API from scratch in C# so that I have something to show my boss. Any other insight anyone could provide would be useful.

rushed commented 7 years ago

Rails expects database schemas to conform to certain conventions. If yours doesn't you'll need to change it, pretend it does via views, or do additional work. This probably isn't a good forum for your question as it doesn't appear that you're describing a problem with activerecord-sqlserver-adapter. You might consider trying StackOverflow, the Rails channel on IRC, searching for articles about working with legacy DBs in Rails, or (as noted earlier in this thread) changing your primary key to be auto incrementing.

PAMulligan commented 7 years ago

What ended up working for me was to create migrations that added the indices that I needed. Make sure to add the name option as well, as outlined here: http://apidock.com/rails/v4.2.1/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index. In the model, make sure to set self.primary_key and self.table_name to the correct values, as well. Now, using FactoryGirl to test my models is working perfectly!