Domanda

The stack trace:

Creating test database for alias 'default'...
Destroying old test database 'default'...
Traceback (most recent call last):

File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "C:\Py27_64\lib\site-packages\django\test\runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "C:\Py27_64\lib\site-packages\django\db\backends\creation.py", line 339, in create_test_db
load_initial_data=False)
File "C:\Py27_64\lib\site-packages\django\core\management\__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Py27_64\lib\site-packages\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Py27_64\lib\site-packages\django\core\management\commands\syncdb.py", line 107, in handle_noargs
cursor.execute(statement)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "C:\Py27_64\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Py27_64\lib\site-packages\django\db\backends\util.py", line 51, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" already exists

My settings.py file:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
            'myApp',
)

My suspicion: in models.py I have manually added class AuthUser() due to earlier misconception of how models.py actually works.

I wanted custom fields/attributes and so I thought this would be the way to do it. Additionally, I actually modified auth_user table in postgres generated by Django to add custom fields as well.

Now, when I am trying to run ./manage.py test myApp I get the above stacktrace. Below is class AuthUser():

class AuthUser(models.Model):
    password = models.CharField(max_length=128, blank=True)
    username = models.CharField(max_length=30, blank=True)
    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    is_staff = models.BooleanField(default=True)
    is_active = models.BooleanField(default=True)
    email = models.CharField(max_length=75, blank=True)
    uuid = models.Uuid()
    role = models.CharField(max_length=20, blank=True)
    curricula = models.ManyToManyField(Curricula,
            through='CurriculaUserMap',
            null=True, 
            blank=True)

    class Meta:
        db_table = 'auth_user'
        get_latest_by = 'id'

    def __unicode__(self):
         return self.username

I have not verified that the above stacktrace is caused by defining class AuthUser() in models.py in context with executing ./manage.py test myApp. My questions are:

  • how do I proceed with current implementation of models.py and successfully execute ./manage.py test myApp and continue with my unit testing?
  • the correct way of how do I fix AuthUser class in models.py with customized fields?
    • Can I fix this by extending Django's User class with a one-to-one relationship to a CustomUser class in models.py
    • Will it also support my ManyToManyField relationship to CurriculaUserMap that I've created in postgres DB?

My concern is that if I make the jump to fix this "the right way"; it's going to break pretty much everything.

thank you for your time!

È stato utile?

Soluzione

So I answered my own questions:

  • There was no other way around the fact that creating an AuthUser class in your models.py was not going to fly. It had to be removed and anywhere in my views.py where I referenced AuthUser had to be updated to point to the Django built-in User object.
  • Creating a CustomUser class in models.py and defining an attribute as models.OneToOneField to the Django built-in User object was easier than anticipated.
    • With regards to the Many-to-Many field I just moved that to the other "end" of the Many-to-Many mapping; it did not have to be defined in the CustomUser object.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top