在Django View中为Postgres添加用户/帐户表 [英] Adding a user/accounts table to Postgres in Django View

查看:92
本文介绍了在Django View中为Postgres添加用户/帐户表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转到编辑2

在views.py中调用以下adduser函数。我先保存用户,因为它的id(由Django在INSERT自动创建)是帐户和密码的主键/外键。添加用户似乎工作正常,但是当它到达 Accounts(user = u)时,会出现以下错误:

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 accounts and passwords. Adding a user seems to be working fine, but then when it gets to the Accounts(user=u), the following error throws:

IntegrityError at /adduser
insert or update on table "OmniCloud_App_accounts" violates foreign key constraint "user_id_refs_id_468fbcec324e93d2"
DETAIL:  Key (user_id)=(4) is not present in table "OmniCloud_App_user".

但是关键应该在那里,因为它只是将用户保存到db ...


But the key should be there since it just saved the user to the db...

def adduser(request):
    username = request.POST['username']
    password = request.POST['password']
    u = User.objects.create_user(username, request.POST['email'], password)
    u.save()
    a = Accounts(user=u)
    p = Passwords(user=u)
    a.save()
    p.save()
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth.login(request, user)
        return HttpResponseRedirect("/%s/" %u.id)
    else:
        return HttpResponseRedirect("/account/invalid/")

编辑:这是帐户初始化的开始:从django.db导入模型


Here is the beginning to the initialization of Accounts:

from django.db import models
from django.contrib.auth.models import User
class Accounts(models.Model):   
    user = models.ForeignKey(User)

编辑2
我已经意识到,通过访问管理页面,当它抱怨4不在数据库中时,那是因为它正在添加第3个用户。添加第4个用户会抛出一个错误,表示没有使用id 5(duh)的用户。我没有看到任何代码会导致它搜索user_id + 1,任何想法

EDIT 2 I've realized, by going to the admin page, that when It complains that 4 isn't in the database, that is because it is adding the 3rd user. Adding the 4th user throws an error that there is no user with id 5 (duh). I don't see anything in the code that would cause it to search for user_id+1, any ideas

到战争的胸部!

推荐答案

我的钱是拼错的名字。我在错误消息中注意到您有

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

此外,在PostgreSQL中使用混合大小写标识符是SO上很棒的声誉来源。它迟早会咬你这个愚蠢的受害者是这里的常客。任何有这个名字的表也许 - 你忘了在OmniCloud_App_user某处的双引号?

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

这是或者保存用户尚未提交的事务。你可以只创建用户(还没有帐号),并检查它是否最终位于表格中的正确数据库中,并使用正确的ID?

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?

如果您知道正在创建用户,那么问题是:外键
constraint user_id_refs_id_468fbcec324e93d2 看看正确的地方?相同的数据库?相同的模式?相同的表?

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';

显示名称中具有user的所有模式中的所有表,不区分大小写。另外,如果该表具有触发器(可能会导致您的问题)以及其中有多少行(通过 ANALYZE 更新)。可以给你一个领导...

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 ...

您还可以使用标准的元命令 \d 命令行客户端(交互式终端) psql

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

我将运行一个测试用例,并让postgres服务器记录它获得的一切。为此设置此参数:

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';

手册关于日志参数


log_statement(枚举)

log_statement (enum)

控制记录哪些SQL语句。有效值为none(off),ddl,mod和all(所有语句)。

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

如何设置参数

这篇关于在Django View中为Postgres添加用户/帐户表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆