2017-12-10 155 views
13

Tôi đang gặp sự cố khi cố gắng sử dụng kho dữ liệu postgres của heroku từ khôi phục cơ sở dữ liệu địa phương mà tôi có. Sử dụng cơ sở dữ liệu postgres được khôi phục Django chạy như bình thường. Nó lấy tất cả các đối tượng và sử dụng các lĩnh vực của họ, vv của chính primay mà không có bất kỳ vấn đề.IntegrityError: giá trị null trong cột "id" cho tất cả các kiểu/trường có ForeignKey sau khi khôi phục postgres từ dump

Nhưng khi nói đến ghi vào cơ sở dữ liệu, tôi nhận được cùng một lỗi trên bảng, bất kể (các) mô hình.

psycopg2.IntegrityError: null value in column "id" violates not-null constraint

Khi tôi đặt lại cơ sở dữ liệu heroku và tạo đối tượng từ một ô trống không có vấn đề gì. Nhưng nếu tôi cố gắng tạo ra bất kỳ đối tượng trên một cơ sở dữ liệu phục hồi, tôi luôn luôn có được điều này null value in column "id" violates not-null constraint


Dưới đây là một sao chép/dán stack trace từ cố gắng để tạo ra một mô hình cơ bản trong Django Admin. Tôi đã chọn ví dụ mô hình này vì không có mã bổ sung nào liên quan đến việc tạo nó. Không có tín hiệu hay gì cả.

Django Version: 2.0 Python Version: 3.6.3

Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

The above exception (null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, Special Class, special-class).) was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper 574. return self.admin_site.admin_view(view)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func 44. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner 223. return view(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view 1553. return self.changeform_view(request, None, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper 62. return bound_func(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view 142. response = view_func(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func 58. return func.get(self, type(self))(*args2, **kwargs2)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view 1450. return self._changeform_view(request, object_id, form_url, extra_context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view 1490. self.save_model(request, new_object, form, not add)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in save_model 1026. obj.save()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in save 729. force_update=force_update, update_fields=update_fields)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in save_base 759. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in _save_table 842. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py" in _do_insert 880. using=using, raw=raw)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method 82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in _insert 1125. return query.get_compiler(using=using).execute_sql(return_id)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql 1280. cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 100. return super().execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers 77. return executor(sql, params, many, context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in exit 89. raise dj_exc_value.with_traceback(traceback) from exc_value

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute 85. return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /admin/fantasy/raceclass/add/ Exception Value: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, Special Class, special-class).


Mô hình từ stack trace (lưu ý lỗi này xảy ra với tất cả các mô hình, không chỉ này [rất cơ bản] một.)

class RaceClass(models.Model): 
    title = models.CharField(max_length=140) 
    slug = models.SlugField(unique=True) 

    def __str__(self): 
     return self.title 

    class Meta: 
     ordering = ['title'] 

Sau đây là cách Tôi khôi phục (d) dữ liệu cục bộ qua heroku:

Tôi đang đổ bộ cơ sở dữ liệu Postgres cục bộ của mình (V ersion 10,0) sử dụng lệnh:

PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump 

Sau đó tải lên AWS, và khôi phục đến một Postgres Datastore (Phiên bản 9.6.5) trên Heroku sử dụng lệnh:

heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL 

Cả hai đều là trực tiếp từ Heroku Tài liệu: https://devcenter.heroku.com/articles/heroku-postgres-import-export


Side lưu ý: tôi đang sử dụng phiên bản 10,0 Postgres địa phương và Heroku Datastore là 9.6.5

+0

Xin vui lòng bạn có thể gửi stack trace đầy đủ cho các lỗi, và mã chạy (đối với bất cứ mô hình mà bạn đang cố gắng để tiết kiệm) khi lỗi xảy ra và lớp mô hình liên kết. Từ việc xem xét điều này, có vẻ như vấn đề là với khôi phục cơ sở dữ liệu của bạn nhưng với những gì bạn đang cố gắng ghi vào cơ sở dữ liệu. – solarissmoke

+0

@solarissmoke Đã thêm dấu vết ngăn xếp và mã mẫu. Đã đi với một trong những cơ bản nhất tôi có thể tìm thấy là vấn đề xảy ra với tất cả chúng. Hy vọng bạn có thể giúp! – taylor

Trả lời

5

Tôi khá chắc chắn rằng điều này là do bạn đang xuất từ ​​Postgres 10 và nhập vào 9. Nó không hoàn toàn thất bại nhưng một số định nghĩa lược đồ (trong trường hợp này các trường ID được tăng tự động) không được nhập chính xác .

tôi có thể nghĩ ra hai lựa chọn:

  1. Cố gắng bán phá giá SQL thô thay vì một định dạng tùy chỉnh:

    PGPASSWORD=mypassword pg_dump --no-acl --no-owner -h localhost -U myuser mydb > mydb.sql 
    

    Bạn không thể sử dụng pg_restore để tải này - thay vào đó bạn phải chạy truy vấn theo cách thủ công sử dụng psql. Một cái gì đó như thế này sẽ hoạt động:

    heroku pg:psql < mydb.sql 
    

    Thông báo trước ở đây là trước tiên bạn cần dọn sạch cơ sở dữ liệu hiện có.

  2. Nếu điều này cũng không thành công thì bạn cần phải xuất từ ​​cùng một phiên bản chính của Postgres mà bạn muốn nhập vào.

3

Các lỗi chủ yếu là thế này:

I'm using Version 10.0 Postgres locally and Heroku Datastore is 9.6.5

Đó là một vấn đề sắp xảy ra. Tôi sẽ cố gắng sử dụng cùng một phiên bản trên cả hai. Ít nhất là cùng một phiên bản chính.

Điều quan trọng đối với hai đặc điểm này là sự ra đời của tiêu chuẩn-SQL IDENTITY columns in Postgres 10, có nghĩa là để thay thế phần lớn các cột nối tiếp. Bạn đã không tiết lộ các định nghĩa bảng, vì vậy tôi chỉ có thể đoán được. Các tính năng IDENTITY trong Postgres 10 sẽ không dịch trở lại Postgres 9.6, mà rất có thể giải thích các giá trị NULL vi phạm trong các thông báo lỗi của bạn.

liên quan:

Các vấn đề liên quan