1

我正在使用 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.py syncdb 时,出现错误

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 查询

创建表“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
    );

创建表“学生”(

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

请帮我。

谢谢和问候, Jayapal D

4

2 回答 2

1

尝试db_column='studentid'从模型中的id列中删除Student

为什么要更改列名?

另外,你为什么不使用自动ID呢?

于 2012-08-10T09:41:39.303 回答
1

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

于 2012-08-10T10:19:52.617 回答