sei-ec-remote / project-4-issues

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

Need help with models relations #224

Closed AlexMcBex closed 1 year ago

AlexMcBex commented 1 year ago

What stack are you using?

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

Django + SQL

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

I have some problems with some models, I don't know what default should I pick for a new User field to make it be the logged in user by default. same thing to make a created_date field

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


from django.contrib.auth.models import User

class Art(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=50)
    type = models.CharField(max_length=50)
    method = models.CharField(max_length=200)
    author_comment = models.CharField(max_length=300)
    likes = models.IntegerField
    description = models.CharField(max_length=300)
    file = models.CharField(max_length=200, default="smile.jpg")
    date = models.DateTimeField(default=now, editable=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return f'{self.title} by {self.author}'

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

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

(django-env) ➜  xketchez git:(v1) ✗ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, main_app, sessions
Running migrations:
  Applying main_app.0006_art_date_art_user...Traceback (most recent call last):
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: column "user_id" contains null values

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/migrations/migration.py", line 130, in apply
    operation.database_forwards(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 108, in database_forwards
    schema_editor.add_field(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 682, in add_field
    self.execute(sql, params)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 199, in execute
    cursor.execute(sql, params)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/alexmcbex/.local/share/virtualenvs/django-env-3fOLF7ra/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "user_id" contains null values

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

something to do with the session maybe

What things have you already tried to solve the problem?

for the User I've put None as default but it's not what I wanted, for date I imported from django.utils.timezone now and set it as default but since I can't migrate I'm not sure it's working atm

Paste a link to your repository here https://github.com/AlexMcBex/Xketchez

asands94 commented 1 year ago

In Your views.py wherever you have that create for Art, make sure inside your form you have:

form.instance.user = self.request.user  # form.instance is the art
AlexMcBex commented 1 year ago

all I have in views.py is the CreateView class

class ArtCreate(CreateView):
    model = Art
    fields = ['title', 'type', 'method', 'comment', 'description']

should I import form?

asands94 commented 1 year ago

Yeah I'm thinking in the same way we did with cat collector

AlexMcBex commented 1 year ago

still gives me the same error

asands94 commented 1 year ago

Did you recently add users to your model or did you always have them?

AlexMcBex commented 1 year ago

I added it after... :')

asands94 commented 1 year ago

NOOOOOOOOOOO

Try dropping your database then

AlexMcBex commented 1 year ago

Like... how? T-T

asands94 commented 1 year ago

One thing you can try is deleting all your migration files except for __init.py__ and make a new database as well as update the database name in your settings.py and then re run make migrations

AlexMcBex commented 1 year ago

OK, It works now, thank you :')