sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Having Trouble Seeding a table that uses a manytomany field #179

Closed skbarbee closed 1 year ago

skbarbee commented 1 year ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

DR

What's the problem you're trying to solve?

I'm trying to seed comics books but the authors, illustrators, and characters are list.

Post any code you think might be relevant (one fenced block per file)

This is my code for the seed file:
--ComicBooks
INSERT INTO api_comicbook   (title, author_id, illustator_id, edition, publisher_id, character_id, release_date, cover, created_at, updated_at) VALUES
('Ironheart: Meant To Fly',[1],[1,2],1,'Marvel',[1],'11/10/2020','https://i.annihil.us/u/prod/marvel/i/mg/d/00/5f91ea568fcc5/clean.jpg', now(), now())

This is my code for the model:
from django.db import models
from django.contrib.auth import get_user_model
from django.core.validators import MinValueValidator
from .author import Author
from .character import Character
from .publisher import Publisher
from .illustrator import Illustrator

class ComicBook(models.Model):
    title = models.CharField(max_length=100)
    edition = models.IntegerField(validators=[MinValueValidator(0)])
    release_date = models.DateField(null=True, blank=True)
    publisher = models.ForeignKey(
      Publisher,
      on_delete=models.CASCADE,
      related_name='published_comics'
    ) 
    authors = models.ManyToManyField('Author')
    illustrators = models.ManyToManyField('Illustrator')
    characters = models.ManyToManyField('Character')
    cover = models.CharField(max_length=200, null=True, blank=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.title}"

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

psql:seed.sql:57: ERROR:  syntax error at or near "["
LINE 2: ('Ironheart: Meant To Fly',[1],[1,2],1,'Marvel',[1],'11/10/2...

What is your best guess as to the source of the problem?

I looked at the data and it seems to me that with the many to many field it basically makes a seperate table to reference them however I do not know how to do that since I need those fields to make the comic

What things have you already tried to solve the problem?

Initially I just had illustrators, publisher, etc but I saw on the actaul table it said publisher_id so I tried changing them to that but that did not work either

I tried putting the list into {} and then {[]} and using the string instead of the integer but none of that work.

Paste a link to your repository here https://github.com/skbarbee/django-comics

skbarbee commented 1 year ago

the many to many fields create a table so I have to first create the comic-book then seed then create the other tables and reference the comicbook pk