sei-ec-remote / team-project-issues

0 stars 0 forks source link

Adding a photo #177

Closed RyanCLuis closed 9 months ago

RyanCLuis commented 9 months ago

Describe the bug A clear and concise description of what the bug is. when i try and add a photo to the page, and error pops up saying "column main_app_photo.pet_id does not exist" but also gives me a hint of "HINT: Perhaps you meant to reference the column "main_app_photo.dog_id"." so somewhere it is trying to get a dog_id but i have nothing in my code the references dog_id. Before, when writing the code we started with dog to make things easier, then we switched over to pet.

What is the problem you are trying to solve? the dtails page wont show a picture.

Expected behavior A clear and concise description of what you expected to happen. when the page renders it is suppose to show no photo has been added, but instead it throws that error

What is the actual behavior? A clear and concise description of what actually happened.

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

{% for photo in pet.photo_set.all %}
    <img class="responsive-img card-panel" src="{{photo.url}}" alt="Cat Photo">
{% empty %}
    <div class="card-panel teal-text center-align">No Photos Uploaded</div>
{% endfor %}
def add_photo(request, pet_id):
    photo_file = request.FILES.get('photo-file', None)
    if photo_file:
        s3 = boto3.client('s3')
        key = uuid.uuid4().hex[:6] + photo_file.name[photo_file.name.rfind('.'):]
        try:
            bucket = os.environ['S3_BUCKET']
            s3.upload_fileobj(photo_file, bucket, key)
            url = f"{os.environ['S3_BASE_URL']}{bucket}/{key}"
            Photo.objects.create(url=url, pet_id=pet_id)
        except Exception as e:
            print('An error occurred uploading file to S3')
            print(e)
    return redirect('detail', pet_id=pet_id)
class Pet(models.Model):
    name = models.CharField(max_length=100)
    species = models.CharField(max_length=100)
    # size = models.CharField(max_length=100)
    age = models.IntegerField()
    # gender = models.CharField(max_length=100)
    shots_received = models.TextField(max_length=500)
    description = models.TextField(max_length=500)
    fixed = models.BooleanField(default=False)

    # Add the foreign key linking to a user
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('detail', kwargs={'pet_id': self.id})

class Photo(models.Model):
    url = models.CharField(max_length=200)
    pet = models.ForeignKey(Pet, on_delete=models.CASCADE)

    def __str__(self):
        return f"Photo for pet_id: {self.pet_id} @{self.url}"

What is your best guess as to the source of the problem? im thinking maybe in the migrations somewhere it still has a dog_id and have no way of deleting that id

What things have you already tried to solve the problem? ive tried deleting all my migrations and migrating again and still throws up the same errors

Additional context Add any other context about the problem here.

Paste a link to your repository here the error is on my branch so inst on the repo https://github.com/RyanCLuis/Fur-Ever-Friends

nayaba commented 9 months ago

Can you look at your actual database in neon and see what the columns are named - is there a dog column?

RyanCLuis commented 9 months ago

image but in the models i have image

nayaba commented 9 months ago

Try to make migrations and then run them

RyanCLuis commented 9 months ago

would that have something to do with i created dog_id first and in my migrations i save it. so i would have to delete the database and set it up over again?

image still the same error image

nayaba commented 9 months ago

You could try dropping the database and migrating again

RyanCLuis commented 9 months ago

do i need to delete all my migrations before? ie 0001_init___ 0002 xxxxx 0003xxxxx?

RyanCLuis commented 9 months ago

i delete the database and re migrate i had to recreate the superuser, but i can still add a pet. when i click on detail i get the same error, just a little different image

nayaba commented 9 months ago

This error is saying that your photo table does not exist. When you ran makemigrations, did it make any migration files or did it tell you there were no changes to be made?

RyanCLuis commented 9 months ago

it said no changed made