Skip to content Skip to sidebar Skip to footer

Adding A User/accounts Table To Postgres In Django View

Go to edit 2 Calling the following adduser function in views.py. I save the user first because it's id (automatically created by Django upon INSERT) is the primary/foreign key for

Solution 1:

My money is on a misspelled name. I notice in the error message that you have

OmniCloud_App_accounts
OmniCloud_App_user

Second table uses singular. There isn't a second table like this by chance:

OmniCloud_App_users

Also, using mixed case identifiers in PostgreSQL is a great source of reputation here on SO. It will bite you sooner or later. Victims of that folly are regulars here. Any table with this name maybe - and you forgot the double quotes in "OmniCloud_App_user" somewhere?

omnicloud_app_user

It's either that or the transaction saving the user has not been committed yet. Can you only create the user (and no accounts yet) and check if it ends up in the right database in the table and with the right ID?

Edit: tools to diagnose the problem

If you know that users are being created, then the question is: does the foreign key constraint user_id_refs_id_468fbcec324e93d2 look at the right place? Same database? Same schema? Same table?

To find out which tables exists in your database, try the following query (if you have the necessary privileges):

SELECT n.nspname AS schema_name
      ,c.relname AS table_name
      ,c.relhastriggers
      ,c.reltuples
FROM   pg_catalog.pg_class c
LEFT   JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relname ~~* '%user%'AND    c.relkind = 'r'AND    nspname <> 'pg_catalog';

Shows all tables in all schemas that have "user" in the name, case insensitive. Plus if the table has triggers (could cause your problem) and how many rows are in it (estimate updated by ANALYZE). Might give you a lead ...

You can also use the meta-command \d of the standard command line client (interactive terminal) psql.

I would run a test case and have the postgres server log everything it gets. Set this parameter for that purpose:

set log_statement = 'all';

The manual about logging-parameters.

log_statement (enum)

Controls which SQL statements are logged. Valid values are none (off), ddl, mod, and all (all statements).

The manual on how to set parameters.

Solution 2:

I can't see anything wrong with the code you have provided that would cause that error, but there are a few things that you're doing differently from the recommended method

You seem to be using a ForeignKey relationship - that may not be what you want, unless you're planning on a single User being linked to multiple Accounts. A OneToOne relationship is probably what you want, unless this is the case.

Using a Handler to create the Account associated with the User seems like a better way to handle things as well - as django will deal with calling the handler, rather than you having to remember to call the appropriate code. Though whether that will fix your problem, I'm not so sure

This part of the manual suggests that you shouldn't need to call u.save() after calling create_user, unless you have altered the user since it was created.

Solution 3:

Try decorating your function this way :

from django.db import transaction

 @transaction.autocommitdefadduser(request):

django default with postgres is to start a transaction with the view and commit it at the end (exceptions are rollbacks).

so .saved() objects are not always the way you expect them...

Solution 4:

few comments Use OneToOneField in accounts modeland userprofile https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.get_profile and make a signal saving for account saving

you can use saving user in this way. Pylint say it's not good but the same way use inside of Django

try:
    user = User.objects.create(**request.POST) #better way save from forms, no the forms is best way except:
    #exception logicpasselse:
    auth.login(request, user)

so if you use forms

form = UserForm(request.POST orNone)
if form.is_valid():
    user = form.save()
    auth.login(request, user)
else:
    #invalid data logicpass

Post a Comment for "Adding A User/accounts Table To Postgres In Django View"