Repository: cookiecutter/cookiecutter-django

Issue:
fresh project runs into `ERROR:  value too long for type character varying(50)`
## What happened?

you can hit the same issue with running pytest or manage.py migrate (inside docker container)

I created a new project with the following settings:
```
cookiecutter https://github.com/pydanny/cookiecutter-django
You've downloaded /Users/elmcrest/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it?
[y/n] (y): n
Do you want to re-use the existing version? [y/n] (y): y
  [1/27] project_name (My Awesome Project): My Awesome Project
  [2/27] project_slug (my_wesome_project): my_wesome_project
  [3/27] description (Behold My Awesome Project!): Behold My Awesome Project!
  [4/27] author_name (Daniel Roy Greenfeld): Daniel Roy Greenfeld
  [5/27] domain_name (example.com): example.com
  [6/27] email (drg@example.com): drg@example.com
  [7/27] version (0.1.0):
  [8/27] Select open_source_license
    1 - MIT
    2 - BSD
    3 - GPLv3
    4 - Apache Software License 2.0
    5 - Not open source
    Choose from [1/2/3/4/5] (1): 5
  [9/27] Select username_type
    1 - username
    2 - email
    Choose from [1/2] (1): 2
  [10/27] timezone (UTC):
  [11/27] windows (n):
  [12/27] Select editor
    1 - None
    2 - PyCharm
    3 - VS Code
    Choose from [1/2/3] (1): 3
  [13/27] use_docker (n): y
  [14/27] Select postgresql_version
    1 - 15
    2 - 14
    3 - 13
    4 - 12
    5 - 11
    6 - 10
    Choose from [1/2/3/4/5/6] (1): 1
  [15/27] Select cloud_provider
    1 - AWS
    2 - GCP
    3 - Azure
    4 - None
    Choose from [1/2/3/4] (1): 3
  [16/27] Select mail_service
    1 - Mailgun
    2 - Amazon SES
    3 - Mailjet
    4 - Mandrill
    5 - Postmark
    6 - Sendgrid
    7 - SendinBlue
    8 - SparkPost
    9 - Other SMTP
    Choose from [1/2/3/4/5/6/7/8/9] (1):
  [17/27] use_async (n): y
  [18/27] use_drf (n):
  [19/27] Select frontend_pipeline
    1 - None
    2 - Django Compressor
    3 - Gulp
    4 - Webpack
    Choose from [1/2/3/4] (1): 4
  [20/27] use_celery (n): y
  [21/27] use_mailhog (n): y
  [22/27] use_sentry (n): y
  [23/27] use_whitenoise (n): y
  [24/27] use_heroku (n):
  [25/27] Select ci_tool
    1 - None
    2 - Travis
    3 - Gitlab
    4 - Github
    5 - Drone
    Choose from [1/2/3/4/5] (1): 4
  [26/27] keep_local_envs_in_vcs (y):
  [27/27] debug (n):
 [SUCCESS]: Project initialized, keep up the good work!
```

then run `docker-compose up` (or start a container/django env else and run into the same issue) and run into:

```
my_wesome_project_local_django        | django.db.utils.DataError:
my_wesome_project_local_django        | value too long for type character varying(50)
```

## What should've happened instead?
not hitting the exception

## Additional details

I tried it again with psycopg[c]==3.1.10 without a immediate difference

- Host system configuration:

  - Version of cookiecutter CLI (get it with `cookiecutter --version`): Cookiecutter 2.3.0 (via homebrew)
  - OS name and version:
  - 
ProductName:		macOS
ProductVersion:		13.4.1
ProductVersionExtra:	(c)
BuildVersion:		22F770820d


  - Python version, run `python3 -V`: 3.11.4 (docker)
  - Docker version (if using Docker), run `docker --version`: Docker version 24.0.2, build cb74dfc
  - docker compose version (if using Docker), run `docker compose --version`:  Docker Compose version v2.19.1 (FYI it's meanwhile docker compose version without double dash)

- Options selected and/or [replay file](https://cookiecutter.readthedocs.io/en/latest/advanced/replay.html):
  On Linux and macOS: `cat ${HOME}/.cookiecutter_replay/cookiecutter-django.json`

```json
{
  "cookiecutter": {
    "project_name": "behold",
    "project_slug": "slug",
    "description": "behold",
    "author_name": "John doe",
    "domain_name": "example.com",
    "email": "",
    "version": "0.1.0",
    "open_source_license": "Not open source",
    "username_type": "email",
    "timezone": "UTC",
    "windows": "n",
    "editor": "VS Code",
    "use_docker": "y",
    "postgresql_version": "15",
    "cloud_provider": "Azure",
    "mail_service": "Mailgun",
    "use_async": "y",
    "use_drf": "n",
    "frontend_pipeline": "Webpack",
    "use_celery": "y",
    "use_mailhog": "y",
    "use_sentry": "y",
    "use_whitenoise": "y",
    "use_heroku": "n",
    "ci_tool": "Github",
    "keep_local_envs_in_vcs": "y",
    "debug": "n",
    "_template": "https://github.com/pydanny/cookiecutter-django",
    "_output_dir": "/Users/user/projects",
    "_repo_dir": "/Users/user/.cookiecutters/cookiecutter-django"
  },
  "_cookiecutter": {
    "project_name": "My Awesome Project",
    "project_slug": "{{ cookiecutter.project_name.lower()|replace(' ', '_')|replace('-', '_')|replace('.', '_')|trim() }}",
    "description": "Behold My Awesome Project!",
    "author_name": "Daniel Roy Greenfeld",
    "domain_name": "example.com",
    "email": "{{ cookiecutter.author_name.lower() | trim() |replace(' ', '-') }}@{{ cookiecutter.domain_name.lower() | trim() }}",
    "version": "0.1.0",
    "open_source_license": [
      "MIT",
      "BSD",
      "GPLv3",
      "Apache Software License 2.0",
      "Not open source"
    ],
    "username_type": [
      "username",
      "email"
    ],
    "timezone": "UTC",
    "windows": "n",
    "editor": [
      "None",
      "PyCharm",
      "VS Code"
    ],
    "use_docker": "n",
    "postgresql_version": [
      "15",
      "14",
      "13",
      "12",
      "11",
      "10"
    ],
    "cloud_provider": [
      "AWS",
      "GCP",
      "Azure",
      "None"
    ],
    "mail_service": [
      "Mailgun",
      "Amazon SES",
      "Mailjet",
      "Mandrill",
      "Postmark",
      "Sendgrid",
      "SendinBlue",
      "SparkPost",
      "Other SMTP"
    ],
    "use_async": "n",
    "use_drf": "n",
    "frontend_pipeline": [
      "None",
      "Django Compressor",
      "Gulp",
      "Webpack"
    ],
    "use_celery": "n",
    "use_mailhog": "n",
    "use_sentry": "n",
    "use_whitenoise": "n",
    "use_heroku": "n",
    "ci_tool": [
      "None",
      "Travis",
      "Gitlab",
      "Github",
      "Drone"
    ],
    "keep_local_envs_in_vcs": "y",
    "debug": "n"
  }
}
```



Discussion:
It fails to create a row in one table, because one `CharField` value is too long for the column in the database. 

You haven't shared a lot of logs, so we don't see which migration fails. However, since you're claiming that it's a fresh project and that the size of the column is 50, I bet it's failing on `sites.0003_set_site_domain_and_name`, which creates the site into the DB table from Django's sites framework:

https://github.com/cookiecutter/cookiecutter-django/blob/bd153d06911f7c2291e96fa0d562f0e58aada1e5/%7B%7Bcookiecutter.project_slug%7D%7D/%7B%7Bcookiecutter.project_slug%7D%7D/contrib/sites/migrations/0003_set_site_domain_and_name.py#L12-L18

The `name` is 50 char long:

https://github.com/cookiecutter/cookiecutter-django/blob/bd153d06911f7c2291e96fa0d562f0e58aada1e5/%7B%7Bcookiecutter.project_slug%7D%7D/%7B%7Bcookiecutter.project_slug%7D%7D/contrib/sites/migrations/0001_initial.py#L31

Did you redact the value of your project name by any chance? Is the actual value longer than 50 char? If that's the case, I suggest you to edit the name of the site in your migration `sites.0003_set_site_domain_and_name`. AFAIK, it's just an internal alias for you project name within the admin, it doesn't affect what the users will see. Let me know it it fixes your issue.

As for a fix to the template itself, we could limit the length of the value in the migration:

```diff
def update_site_forward(apps, schema_editor):
    """Set site domain and name."""
    Site = apps.get_model("sites", "Site")
    _update_or_create_site_with_sequence(
        Site,
        schema_editor.connection,
        "{{cookiecutter.domain_name}}",
-       "{{cookiecutter.project_name}}",
+       "{{cookiecutter.project_name[:50]}}",
    )
```
alright, just a quick update. I've created another project with the same settings but a different project name (shorter). 

The above error is gone and I have 26 passing tests.

I'll investigate it further later today or tomorrow to make sure it was related to the length of the project name, just FYI the exception was thrown on multiple occasions including your suspected sites app.

so far, thx for the response.
now I've recreated the project with exactly the same inputs as yesterday but I couldn't reproduce the issue. so whatever it was, it was fixed by one of the last commits in between my try yesterday and today.

since I'm not able anymore to reproduce I'll just close this issue.

Resolving patch:
diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/contrib/sites/migrations/0003_set_site_domain_and_name.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/contrib/sites/migrations/0003_set_site_domain_and_name.py
--- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/contrib/sites/migrations/0003_set_site_domain_and_name.py
+++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/contrib/sites/migrations/0003_set_site_domain_and_name.py
@@ -40,8 +40,8 @@ def update_site_forward(apps, schema_editor):
     _update_or_create_site_with_sequence(
         Site,
         schema_editor.connection,
-        "{{cookiecutter.domain_name}}",
-        "{{cookiecutter.project_name}}",
+        "{{ cookiecutter.domain_name }}",
+        "{{ cookiecutter.project_name[:50] }}",
     )
 
 


Test patch: