sei-ec-remote / team-project-issues

0 stars 0 forks source link

Assigning a User to Dreams #182

Closed plusflora closed 7 months ago

plusflora commented 7 months ago

Describe the bug A clear and concise description of what the bug is. In attempting to assign dreams to specific users, I'm now getting broken pages and am unable to makemigrations/migrate to attempt to fix this.

What is the problem you are trying to solve? Assigning dreams to users (models.py under the Dream/DreamForm models)

Expected behavior A clear and concise description of what you expected to happen. the page correctly showing.

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

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

  # Adds an owner field to link the dream to a user
  owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='dreams', default=None)

What is your best guess as to the source of the problem? having dreams in the database without owners.

What things have you already tried to solve the problem? I've tried to retroactively assign them a user but that doesn't want to work.

Additional context Add any other context about the problem here. I've kinda looped around a few times back to the same point.

Paste a link to your repository here https://github.com/plusflora/Project3-DreamHues/tree/vince

nayaba commented 7 months ago

When you look in your actual database on neon, does the dream table have an owner_id column? Also, what happens when you attempt to make migrations? (and are these migrations meant to add the owner_id column?)

plusflora commented 7 months ago

I can't see the neon database, I'm not the owner of it. I'll hit up Jacob. But in the meantime - these are supposed to add the owner_id and then retroactively assign the dreams currently in the database. if deleting the current dreams from the database would fix this I can do that.

nayaba commented 7 months ago

Can you send the error message you get when you run the makemigrations command?

plusflora commented 7 months ago

The last change I did has makemigrations working - but migrate doesn't. this is the error that I got from that.

Running migrations: Applying main_app.0007_dream_owner...Traceback (most recent call last): File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) psycopg2.errors.NotNullViolation: column "owner_id" of relation "main_app_dream" contains null values

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/vbouq/sei/projects/Project3-DreamHues/Project3-DreamHues/Project3DreamHues/manage.py", line 22, in main() File "/home/vbouq/sei/projects/Project3-DreamHues/Project3-DreamHues/Project3DreamHues/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/usr/lib/python3/dist-packages/django/core/management/init.py", line 419, in execute_from_command_line utility.execute() File "/usr/lib/python3/dist-packages/django/core/management/init.py", line 413, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 354, in run_from_argv self.execute(*args, cmd_options) File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 398, in execute output = self.handle(*args, *options) File "/usr/lib/python3/dist-packages/django/core/management/base.py", line 89, in wrapped res = handle_func(args, kwargs) File "/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py", line 244, in handle post_migrate_state = executor.migrate( File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/usr/lib/python3/dist-packages/django/db/migrations/executor.py", line 227, in apply_migration state = migration.apply(state, schema_editor) File "/usr/lib/python3/dist-packages/django/db/migrations/migration.py", line 126, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/usr/lib/python3/dist-packages/django/db/migrations/operations/fields.py", line 104, in database_forwards schema_editor.add_field( File "/usr/lib/python3/dist-packages/django/db/backends/base/schema.py", line 522, in add_field self.execute(sql, params) File "/usr/lib/python3/dist-packages/django/db/backends/base/schema.py", line 145, in execute cursor.execute(sql, params) File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 79, in _execute with self.db.wrap_database_errors: File "/usr/lib/python3/dist-packages/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) django.db.utils.IntegrityError: column "owner_id" of relation "main_app_dream" contains null values

nayaba commented 7 months ago

Try changing your default from None to 1

plusflora commented 7 months ago

We're talking about the model right? So like this?

class Dream(models.Model):
  # all that other code
  # Adds an owner field to link the dream to a user
  owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='dreams', default=1)

I did that and I still got this Operations to perform: Apply all migrations: admin, auth, contenttypes, main_app, sessions Running migrations: Applying main_app.0007_dream_owner...Traceback (most recent call last): File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) psycopg2.errors.NotNullViolation: column "owner_id" of relation "main_app_dream" contains null values

plusflora commented 7 months ago

It let me makemigration but it won't let me migrate.

timmshinbone commented 7 months ago

Easiest thing to do would be to drop that table for dreams and then run the migrations. The problem is that the newly added owner field would be missing values for older entries in your db. Adding a default doesn't always work, as the default usually just applies that to future entries.

My recommendation: Go into the admin portal and copy all of the dreams you've added so far and paste them into a note or something, that way it'll be easy to add them back once the table is updated.