Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Column user_id is not Unique #113

Closed
fernandoferreira-me opened this issue Aug 8, 2013 · 13 comments
Closed

Column user_id is not Unique #113

fernandoferreira-me opened this issue Aug 8, 2013 · 13 comments

Comments

@fernandoferreira-me
Copy link

I have the following models

app1.models.py

class UserProfile(Subject):
"""
UserProfile class
"""
# This field is required.
user = models.OneToOneField(User)
# Other fields here
company = models.CharField(max_length=50, null=True, blank=True,
verbose_name=("Company"))
contact = models.CharField(max_length=50, null=True, blank=True,
verbose_name=
("Contact"))
msg = models.TextField(null=True, blank=True, verbose_name=_("Message"))

def __unicode__(self):
    return self.user.username

class ParentImportJob(models.Model):
"""
Class to store importing jobs
"""

STATUS_ACTIVE = u'A'
STATUS_SUCCESS = u'S'
STATUS_PARTIAL = u'P'
STATUS_ERROR = u'E'

STATUS_CHOICES = (
    (STATUS_ACTIVE, _(u'In Progress')),
    (STATUS_SUCCESS, _(u'Successfully Imported')),
    (STATUS_PARTIAL, _(u'Partially Imported')),
    (STATUS_ERROR, _(u'Aborted with error')),
)

status = models.CharField(max_length=1, choices=STATUS_CHOICES)
user_profile = models.ForeignKey(UserProfile)
errors = models.TextField(null=True, blank=True)
start_date = models.DateTimeField(auto_now_add=True)
end_date = models.DateTimeField(blank=True, null=True)
instance_class = models.CharField(max_length=200)

app2.models.py

class ImportJob(ParentImportJob):
"""
Class to store jobs of files being imported
Extends ParentImportJob
ParentImportJob is not abstract! But I am interested in 2 separated tables
"""

_imported_file = models.TextField(null=True,
                                  blank=True,
                                  db_column='imported_file')
import_result = models.TextField(null=True, blank=True)

def set_import_file(self, imported_file):
    """ Set method for import_file field """
    self._imported_file = base64.encodestring(imported_file)

def get_import_file(self, imported_file):
    """ Set method for import_file field """
    return base64.decodestring(self._imported_file)

imported_file = property(get_import_file, set_import_file)

The following recipe:

ob_mock = Recipe(ImportJob,
import_file=ofile.read(),
import_result=EXCEL_DICT)

When I ran self.job = mommy.make_recipe('excel2db.job_mock') inside the testcase I get IntegrityError: column user_id is not unique

I'm doing something wrong?

@vandersonmota
Copy link
Collaborator

As far as i read, there is nothing wrong with your code. I'll mark this as a bug.

I believe it has something to do with OneToOneField, but i'll take a closer look.

Thanks for the report. =)

@fernandoferreira-me
Copy link
Author

I have a follow-up. When I try to mock the UserProfile model, I have the same problem.

    user_mock = Recipe(UserProfile)
    user_profile=mommy.make_recipe('excel2db.user_mock')

IntegrityError: column user_id is not unique

@vandersonmota
Copy link
Collaborator

which Django and model_mommy versions are you using?

@fernandoferreira-me
Copy link
Author

Django==1.4.5
model-mommy==1.0

@vandersonmota
Copy link
Collaborator

does this bug also happens with the latest? (master branch)

@fernandoferreira-me
Copy link
Author

Am I doing it wrong?

pip install -e git+https://github.com/vandersonmota/model_mommy.git@f734decb017e537b42993ceb95768f489971b267#egg=model_mommy-dev

 from model_mommy import Recipe

 ---------------------------------------------------------------------------
 ImportError                               Traceback (most recent call last)
 <ipython-input-1-61cba1667a5e> in <module>()
 ----> 1 from model_mommy.recipe import Recipe
 ImportError: cannot import name Sequence

@vandersonmota
Copy link
Collaborator

the correct import is

from model_mommy.recipe import Recipe

@fernandoferreira-me
Copy link
Author

Sure! I've pasted the wrong line... The result is the same

n [1]: from model_mommy.recipe import Recipe


ImportError Traceback (most recent call last)
in ()
----> 1 from model_mommy.recipe import Recipe

/Users/fguimara/.virtualenvs/twist-integrator/src/model-mommy/model_mommy/recipe.py in ()
1 #coding: utf-8
2 import inspect
----> 3 import mommy
4 from exceptions import RecipeNotFound
5

/Users/fguimara/.virtualenvs/twist-integrator/src/model-mommy/model_mommy/mommy.py in ()
24 import generators
25 from exceptions import ModelNotFound, AmbiguousModelName, InvalidQuantityException
---> 26 from recipe import Sequence
27
28 recipes = None

ImportError: cannot import name Sequence

@vandersonmota
Copy link
Collaborator

Sorry, just pushed the fix. Try it again.

thanks!

@fernandoferreira-me
Copy link
Author

So, for
pip install -e git+https://github.com/vandersonmota/model_mommy.git@f734decb017e537b42993ceb95768f489971b267#egg=model_mommy-dev

and Django==1.4.6

The error persists:

(...)
model-mommy/model_mommy/mommy.pyc in instance(self, attrs, _commit)
288 # m2m only works for persisted instances
289 if _commit:
--> 290 instance.save()
291 self._handle_m2m(instance)
292 return instance

(...)
django/db/backends/sqlite3/base.pyc in execute(self, query, params)
342 query = self.convert_query(query)
343 try:
--> 344 return Database.Cursor.execute(self, query, params)
345 except Database.IntegrityError, e:
346 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]

IntegrityError: column user_id is not unique

@vandersonmota
Copy link
Collaborator

I created a UserProfile model here pointing (OneToOne) to django.contrib.auth.User and i didn't got this error. This also happens if you try to create the UserProfile without model_mommy?

@tkrajca
Copy link

tkrajca commented Sep 9, 2013

Hi, I've got a similar problem, we have a post_save hook that creates a user profile as soon as a user is created. So, then when model_mommy is to generate a user profile, it first correctly generates the user instance but then the user profile is automatically created by the post_save hook, so then when mommy tries to create the user profile with oneToOne field to the already created user, it gets a unique key error because the User profile has already been created.

I have a fix for this, I am about to create a pull request.

tkrajca pushed a commit to tkrajca/model_mommy that referenced this issue Sep 9, 2013
…rom a related field

or by some other means, it might throw an unique exception when it is created
again in Mommy.instance(). The exception is thrown if the to-be created
instance has OneToOneField or some other field that is set (in **attrs) and
is unique.
@limdauto
Copy link

limdauto commented Apr 3, 2014

@tkrajca's fix works for me :) Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants