2012-02-15 8 views
1

トレースバックは '' 'python manage.py syncdb' ''にあり、パスワードが2回入力された直後に発生します。データベースはsqlite3です。syncdbを試してみると、重複したuser_idエラーはどこから来ますか?

トレースバックは、次のとおりです。

Traceback (most recent call last): 
    File "manage.py", line 14, in <module> 
    execute_manager(settings) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager 
    utility.execute() 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle 
    return self.handle_noargs(**options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 109, in handle_noargs 
    emit_post_sync_signal(created_models, verbosity, interactive, db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/sql.py", line 190, in emit_post_sync_signal 
    interactive=interactive, db=db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site- packages/django/dispatch/dispatcher.py", line 172, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 70, in create_superuser 
    call_command("createsuperuser", interactive=True) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 166, in call_command 
return klass.execute(*args, **defaults) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 134, in handle 
    User.objects.create_superuser(username, email, password) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 140, in create_superuser 
    u = self.create_user(username, email, password) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 136, in create_user 
    user.save(using=self._db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 570, in save_base 
    created=(not record_exists), raw=raw, using=using) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send 
    response = receiver(signal=self, sender=sender, **named) 
    File "/Users/jonathan/pim/accounts/models.py", line 12, in create_user_profile 
    UserProfile.objects.create(user = instance) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 138, in create 
    return self.get_query_set().create(**kwargs) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 360, in create 
    obj.save(force_insert=True, using=self.db) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 553, in save_base 
    result = manager._insert(values, return_id=update_pk, using=using) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 195, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 1436, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql 
    cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.IntegrityError: column user_id is not unique 

私のmodels.pyは以下の通りです:すべてのUSER_PROFILEの参照からアンダースコアを削除

from django.contrib.auth.models import User 
from django.db.models.signals import post_save 
from django.db.models import ForeignKey 
from django.db import models 
from accounts.models import UserProfile 

class CalendarEvent(models.Model): 
    description = models.TextField() 
    user_profile = models.ForeignKey(UserProfile) 
    when = models.TextField() 
    def __init__(self, when, description, user_profile): 
     self.when = when 
     self.description = description 
     self.user_profile = user_profile 
    def accepts(timestamp): 
     if isinstance(timestamp, float): 
      timestamp = time.asctime(time.localtime(timestamp)) 
     if timestamp[:4] == 'days': 
      pass 
     else: 
      if re.match(self.when, timestamp): 
       return True 
      else: 
       return False 

は、この動作を変更しません。

私はここで間違っていますが、どうすれば修正できますか?

--EDIT--

プロジェクトのソースはhttp://JonathansCorner.com/project/pim.tgzです。

+1

作成したアカウントアプリのmodel.pyを投稿して、あなたのUserProfileで行ったことを確認できますか?また、あなたの 'CalendarEvent .__ init __()'であなたのスーパークラスの '__init __()'を呼び出さないように見えます。 – jdi

+0

私にとっては、CalendarEventクラスがあなたが受け取ったエラーの原因ではないようです。他のモデルのuser_id属性にエラーがあるようです(アカウントアプリケーションのUserProfileなど)。 – Jingo

+0

私は@jdiに同意します - あなたの '__init__'メソッドは不要です。あなたが達成しようとしていることは明確ではありません。 Djangoは、モデルフィールドを宣言するときに背後で多くの作業を行います。したがって、通常の属性と同じ方法で '__init__'メソッドでそれらを設定することはできません。 – Alasdair

答えて

1

アカウントモデルモジュールを2つの異なるパスにインポートするため、ユーザーpost_save信号を2回接続しています。つまり、新しいスーパーユーザー用の2つのユーザープロファイルを作成しようとしていて、2回目に失敗します。

Djangoがアカウントアプリをインポートするとき、モデルのパスはaccounts.modelsです。しかし、djangoがpim 'app'をインポートすると(それは実際にはアプリケーションではありません。それはプロジェクトルートです。後で詳しく説明します)、accounts.modelsモジュールはpim.accounts.modelsとして再度インポートされます。

ポストセーブ信号を接続するときは、unique identifierを使用するのが最も簡単な方法です。

post_save.connect(create_user_profile, sender = User, dispatch_uid="create_user_profile") 

より良い修正があなたの輸入と、プロジェクトのレイアウトを整理することです:

  • 代わりに、プロジェクトのルートにそれを置くのCalendarEventモデルが含まれているあなたのmodels.py用に別のアプリを作成します。
  • アプリプロジェクトあなたがあれば
  • はおそらくいくつかをごmodels.py、admin.pyを移動し、
  • たい場合は、「PIM」内の「PIM」を名前を付けることができます場合は、新しいアプリ

にあなたのURLを現在開発中の場合は、ユーザプロファイルテーブルを削除してsyncdbに再作成させるのが最も簡単です。あるいは、シェル内の不要なユーザープロファイルを削除することもできます。

+0

私の答えが編集されたあと、これを追加しました:-) – jdi

+0

あなたは編集編集保存ボタンしかし、私がそれをタップし始めたときにあなたはまだシグナルを言及していませんでした:-) – Alasdair

+0

save.connect()コールを置くのに最適な場所は何ですか?私はそれをadmin.pyに入れ、urls.pyによってインポートされ、Userのインポートについて不平を言います(インポートは 'django.db.models import user'からです)。どこにこれを置くべきですか?何か(もしあれば)他に何をしてコードの行に一致させるべきですか? – JonathanHayward

1

注:私の前の回答がこの正解の代わりに削除されました...

問題は、あなたのpost_save信号が複数回にaccounts.modelsが何によってインポートされるたびに接続されていることです。あなたが実際にやってみたいことはあなたのアカウントのアプリ内

アカウント/ models.py

from django.contrib.auth.models import User 
from django.db import models 

class UserProfile(models.Model): 
    name = models.TextField() 
    scratchpad = models.TextField() 
    user = models.OneToOneField(User) 
    timezone_offset = models.IntegerField(default = 0) 


def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     UserProfile.objects.create(user = instance) 

アカウント/管理をmanagement.pyファイルへの信号接続を再配置です。PY

from django.db.models.signals import post_save 
from accounts.models import create_user_profile 
from django.contrib.auth.models import User 

post_save.connect(create_user_profile, sender = User) 

Djangoはmanagement.pyモジュール1時間をロードしますので、接続されている唯一の信号に関係なく、あなたのaccounts.modelsがインポートされた回数の、今そこにあります。

+0

私はちょうど私の答えを修正しました。以前のものは間違っていた。はい、私はすべてが本当にアプリでなければならないことに同意し、ルートにモデルはありません。しかし本当の理由は、彼の信号が何度もつながっているということです。 – jdi

+0

これらの2つの答えは私が正しい答えを1つだけマークすることができることを後悔します。ありがとう、とアプリに分割する。 – JonathanHayward

+0

あなたは1つの答えを正しいとマークすることができますが、彼の答えはまだupvoteすることができます;) – Alasdair

関連する問題