gpt4 book ai didi

python - 我的 models.py 有什么问题

转载 作者:行者123 更新时间:2023-12-01 05:57:11 25 4
gpt4 key购买 nike

我正在使用 Django 1.4 和 PostgreSQL 9.1.4

我的 models.py 有什么问题

class UserProfile(models.Model):
ROLES_TYPE = (
('Employee', 'Employee'),
('Employor', 'Employor'),
)
user = models.OneToOneField(User, related_name='Profile')
role = models.CharField(max_length=15, choices=ROLES_TYPE)

class Student(models.Model):
id = models.AutoField(db_column='studentid', primary_key=True)
username = models.OneToOneField(UserProfile, related_name='Employee')
first_name = models.CharField(db_column='fname', max_length=100,
null=True, blank=True)
last_name = models.CharField(db_column='lname', max_length=100,
null=True, blank=True)

当我运行 ./manage.pysyncdb 时,出现错误

Creating tables ...
Creating table Student
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 102, in handle_noargs
cursor.execute(statement)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
return self.cursor.execute(sql, params)
File "/home/jayapaldharmamani/Projects/jayapal/aplink/aplink_env/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
return self.cursor.execute(query, args)
django.db.utils.DatabaseError: column "id" referenced in foreign key constraint does not exist

删除相关名称和ID字段后的SQL查询

CREATE TABLE "admin_userprofile" (

 "id" serial NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED,
"role" varchar(15) NOT NULL
);

CREATE TABLE "Student" (

       "id" serial NOT NULL PRIMARY KEY,
"username_id" integer NOT NULL UNIQUE REFERENCES "admin_userprofile" ("id") DEFERRABLE INITIALLY DEFERRED
);

请帮助我。

谢谢和问候,贾亚帕尔 D

最佳答案

这是您的数据库产生的问题,而不是 django 本身。尝试重新创建数据库架构(您可以删除所有相关列并执行 ./manage.pysyncdb

关于python - 我的 models.py 有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com