0

I am trying to populate a postgresql database with initial values using fixtures in django. I keep getting these weird Could not load publication.Article(pk=None): value too long for type character varying(100) errors even though my model looks like this:

class Article(models.Model):
    _id = models.CharField(max_length=1000)
    author_name = models.CharField(max_length=1000)
    caption = models.CharField(max_length=1000)
    isGraphic = models.BooleanField(max_length=1000, default=True)
    pictures = models.URLField(max_length=1000)
    text = models.CharField(max_length=10000)
    title = models.CharField(max_length=1000)
    user_img = models.URLField(max_length=1000)
    videoname = models.CharField(max_length=1000)
    vimeo_id = models.IntegerField(max_length=1000)

Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle self.loaddata(fixture_labels) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata self.load_label(fixture_label) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label obj.save(using=self.using) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save models.Model.save_base(self.object, using=using, raw=True) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 698, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 731, in _do_insert using=using, raw=raw) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 920, in execute_sql cursor.execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) django.db.utils.DataError: Problem installing fixture '/Users/sam.royston/PycharmProjects/sahelien_d/sahelien_django/fixtures/test.json' : Could not load publication.Article(pk=None): value too long for type character varying(100)

why am I getting this error?

test.json:

[
    { "model" : "publication.Article" , "fields": 
         { 
           "_id" : "5306dfa9ed2379f03a000001" , 
           "author_name" : "Sahélien Tombouctou", 
           "caption" : "Les  n’ont fait aucune victime, ni de dégâts  matériels",  
           "isGraphic" : false,  
           "pictures" : [], 
           "text" : "La ville de Tombouctou a reçu des tirs d'obus dans la nuit de dimanche. \n<br>\n<br>\nLes deux premiers obus sont tombés dans la localité de Kabara, à 10km de la cité des 333 saints. Le troisième obus est tombé sur la route de Goundam.\n<br>\n<br>\nLes tirs n’ont fait aucune victime, ni de dégâts matériels. Selon le lieutenant-colonel Seydou Koné, en poste à Tombouctou, l'armée malienne est mobilisée pour déterminer l'origine de cette attaque.",                       
           "title" : "Tombouctou attaquée à la roquette", 
           "videoname" : "okok.mp4", 
            "vimeo_id" : "87246621"
         } 
    }
]
samfr
  • 656
  • 1
  • 6
  • 19

1 Answers1

0

Your json fixture is missing a primary key. Django automatically adds a primary key to your models; called id. As this key is required, you should provide it in fixtures.

The fixture you have posted is does not have this key, you should add it:

[
    { "model" : "publication.Article" , "fields": 
         { 
           "id": "1",
           "_id" : "5306dfa9ed2379f03a000001" , 
           "author_name" : "Sahélien Tombouctou", 
           "caption" : "Les  n’ont fait aucune victime, ni de dégâts  matériels",  
           "isGraphic" : false,  
           "pictures" : [], 
           "text" : "La ville de Tombouctou a reçu des tirs d'obus dans la nuit de dimanche. \n<br>\n<br>\nLes deux premiers obus sont tombés dans la localité de Kabara, à 10km de la cité des 333 saints. Le troisième obus est tombé sur la route de Goundam.\n<br>\n<br>\nLes tirs n’ont fait aucune victime, ni de dégâts matériels. Selon le lieutenant-colonel Seydou Koné, en poste à Tombouctou, l'armée malienne est mobilisée pour déterminer l'origine de cette attaque.",                       
           "title" : "Tombouctou attaquée à la roquette", 
           "videoname" : "okok.mp4", 
            "vimeo_id" : "87246621"
         } 
    }
]

You are missing key fields that are required in your model from your fixture. You need to add user_img and pictures cannot be empty.

The fixture needs to pass all the validation rules of your model; and since all fields are required as per your model, they all need to be available in the fixture.

In addition, you have a max_length argument for integer, boolean and url fields which are not applicable.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • unfortunately this change yields the same issue: `Could not load publication.Article(pk=1): value too long for type character varying(100)` perhaps there is something else wrong with my setup – samfr Jan 28 '15 at 05:25