google-code-export / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Saving a django.contrib.auth.models.User does not populate the id field #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm flabbergasted. 

When I save a django.contrib.auth.models.User in sqlite3, immediately after 
save I have the id 
for the newly inserted user into auth_user available in the model.

But when using SQL Server 2005 using django-pyodbc, if I save a User and ask 
for the id right 
after saving - I get None.

Other models that I created do work just fine - returning the id right after 
save - both in sqlite 
and SQL Server 2005. This seems to only happen with User?!?

What steps will reproduce the problem?

from django.contrib.auth.models import User
user=User(first_name='Joe', last_name='Blow', email='joe@blow.com', 
username='joeblow')
user.id # Returns None as it should
user.save()
user.id # Returns None?!? Why?

from myapp import models
mymodel=models.MyModel(field='blah')
mymodel.id # Returns None as it should
mymodel.save()
mymodel.id # Returns the id for the newly inserted model

What is the expected output? What do you see instead?

user.id after a save should return the id for the saved user.

What version of the product are you using? On what operating system?

svn revision 173

Please provide any additional information below.

Original issue reported on code.google.com by ce.lo...@gmail.com on 5 Nov 2009 at 2:58

GoogleCodeExporter commented 9 years ago
Please close this one.

The problem is: I have more than one database and Django still lacks multiple 
database support (coming, I 
know).

So in Database B I'm using a synonym to the dbo.auth_user table in Database A. 
Because I want to share the 
same users between applications accessing different databases! This synonym 
makes it possible for all 
operations on B.dbo.auth_user to work on my App when it is really accessing 
database A via synonym.

Except... SELECT CAST(IDENT_CURRENT(%s) as bigint) % [table_name] does not work 
on synonyms... It seems 
to always return null.

The workaround for me, for the time being, is to right after writing to 
auth_user, I need to reload it from the 
DB. 

Unless someone has a better idea.

Sorry for jumping the gun and posting an error here. It still might be useful 
to someone else that might be 
using the same shenanigans...

Thanks! 

Original comment by ce.lo...@gmail.com on 5 Nov 2009 at 3:11

GoogleCodeExporter commented 9 years ago
Another workaround: use a view instead of a synonym. It works fine.

Original comment by ce.lo...@gmail.com on 5 Nov 2009 at 4:19

GoogleCodeExporter commented 9 years ago

Original comment by cra...@gmail.com on 19 Nov 2009 at 9:54