我的models.py有什么问题

时间:2012-08-10 09:31:02

标签: python django django-models django-1.4

我正在使用带有PostgreSQL 9.1.4的Django 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查询

  

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

创建表“学生”(

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

请帮帮我。

谢谢&amp;问候, Jayapal D

2 个答案:

答案 0 :(得分:1)

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

为什么要更改列名?

另外,为什么你没有使用auto-id呢?

答案 1 :(得分:1)

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