मैं एक Django ऐप पर पोस्टग्रेस के साथ काम कर रहा हूं और मैं एक मॉडल बदलना चाहता हूं, एक पंक्ति को रीसेट कर रहा हूं, इसलिए यह अब प्राथमिक कुंजी नहीं है।Django माइग्रेशन: एक पंक्ति बनाना प्राथमिक कुंजी नहीं है, "प्रोग्रामिंग त्रुटि: एकाधिक डिफ़ॉल्ट मान" प्राप्त करना?
यह मेरा मॉडल वर्तमान में Django में ऐसा दिखाई देता है:
class Meeting(TimeStampedModel):
row = models.CharField(max_length=20, primary_key=True)
total_items = models.IntegerField()
मैं django-admin.py flush
चलाने डेटाबेस से सभी डेटा को हटाने के लिए। अगर मैं python manage.py makemigrations
चलाता हूं तो मुझे No changes detected
दिखाई देता है। तो हम एक स्वच्छ आधार से शुरू कर रहे हैं।
अब मैं models.py में row
संपादित करें जिससे अब वह प्राथमिक कुंजी है:
row = models.CharField(max_length=20)
और python manage.py makemigrations
चलाने के लिए और सेट 1
डिफ़ॉल्ट के रूप में जब उनसे पूछा गया:
You are trying to add a non-nullable field 'id' to meeting without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> 1
Migrations for 'frontend':
0007_auto_20150305_1129.py:
- Add field id to meeting
- Alter field row on meeting
यही लगता है ठीक चलाओ तो अगर मैं python manage.py migrate
चलाएँ:
$ python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: debug_toolbar
Apply all migrations: contenttypes, frontend, sites, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying frontend.0007_auto_20150305_1129...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
...
File "/Users/me/.virtualenvs/meetings/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "frontend_meeting"
यह मुझे क्यों कह रहा है मैं multiple default values
है?
क्या मुझे डिफ़ॉल्ट के रूप में एक अद्वितीय प्राथमिक कुंजी मान सेट करने की आवश्यकता है - और यदि हां, तो मैं यह कैसे कर सकता हूं?
class Migration(migrations.Migration):
dependencies = [
('frontend', '0006_auto_20150305_1050'),
]
operations = [
migrations.AddField(
model_name='meeting',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, default=1, serialize=False, verbose_name='ID'),
preserve_default=False,
),
migrations.AlterField(
model_name='meeting',
name='row',
field=models.CharField(max_length=20),
preserve_default=True,
),
]
मुझे बस अपने पूर्व-मौजूदा माइग्रेशन को हटाना पड़ा। –