1

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

CREATE TABLE "学生" (

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

私を助けてください。

よろしくお願いいたします。 ジャヤパル D

4

2 に答える 2

1

これは、django 自体ではなく、データベースによって生成される問題です。データベース スキーマを再作成してみてください (関連するすべての列を削除して./manage.py syncdb.

于 2012-08-10T10:19:52.617 に答える
1

モデルの列から削除db_column='studentid'してみてください。idStudent

なぜ列名を変更するのですか?

また、なぜauto-idを使用していないのですか?

于 2012-08-10T09:41:39.303 に答える