René's URL Explorer Experiment


Title: Migrations with UUID and server_default fail. · Issue #491 · fastapi/sqlmodel · GitHub

Open Graph Title: Migrations with UUID and server_default fail. · Issue #491 · fastapi/sqlmodel

X Title: Migrations with UUID and server_default fail. · Issue #491 · fastapi/sqlmodel

Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I already searched in Google "How to X in...

Open Graph Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I...

X Description: First Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated searc...

Opengraph URL: https://github.com/fastapi/sqlmodel/issues/491

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Migrations with UUID and server_default fail.","articleBody":"### First Check\n\n- [X] I added a very descriptive title to this issue.\n- [X] I used the GitHub search to find a similar issue and didn't find it.\n- [X] I searched the SQLModel documentation, with the integrated search.\n- [X] I already searched in Google \"How to X in SQLModel\" and didn't find any information.\n- [X] I already read and followed all the tutorial in the docs and didn't find an answer.\n- [X] I already checked if it is not related to SQLModel but to [Pydantic](https://github.com/samuelcolvin/pydantic).\n- [X] I already checked if it is not related to SQLModel but to [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy).\n\n### Commit to Help\n\n- [X] I commit to help with one of those options 👆\n\n### Example Code\n\n```python\nimport uuid\r\nfrom typing import Optional\r\nfrom sqlmodel import SQLModel, Field\r\nfrom sqlalchemy.sql.expression import func\r\nclass Event(SQLModel, table=True):\r\n\r\n    event_uuid: Optional[uuid.UUID] = Field(\r\n        primary_key=True, nullable=False, sa_column_kwargs=dict(server_default=func.gen_random_uuid())\r\n    )\r\n\r\n# a PostgreSQL database with this model cannot have a migration created.\n```\n\n\n### Description\n\nModels which use `sqlmodel.sql.sqltypes.GUID` and a `server_default` cannot be upgraded by Alembic, using PostgreSQL\r\n\r\n### Problem\r\n\r\n1. create a column with UUID and server default, e.g. \r\n   ```\r\n   event_uuid: Optional[uuid.UUID] = Field(\r\n        primary_key=True, nullable=False, sa_column_kwargs=dict(server_default=func.gen_random_uuid())\r\n    )\r\n    ```\r\n2. `alembic` creates a table like this:\r\n   ```\r\n   sa.Column(\r\n            'event_uuid', sqlmodel.sql.sqltypes.GUID(), server_default=sa.text('gen_random_uuid()'), nullable=False\r\n        ),\r\n    ```\r\n3. update the database.  Use the database.  All is fine.\r\n4. Create another migration with alembic.  The analysis and comparison of the current model with the current database will result in the following error:\r\n   ```\r\n   sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type uuid: \"gen_random_uuid()\"\r\n   LINE 1: SELECT gen_random_uuid() = 'gen_random_uuid()'\r\n   ```\r\n\r\n### Workaround   \r\n\r\n   Replacing the `Field` specification in the model with\r\n   ```\r\n   Field(\r\n        sa_column=sa.Column(sqlalchemy.dialects.postgresql.UUID(), primary_key=True, server_default=func.gen_random_uuid()),\r\n    )\r\n   ```\r\n   gets rid of the error.  But this bypasses SqlModel's type inference with a more lengthy and explicit `sa.Column`.\r\n   \r\n   It would appear that sqlmodel.sql.sqltypes as a type in a `sa.Column` somehow does not behave the same as the type class from sqlalchemy.\r\n    \r\n  \r\n \n\n### Operating System\n\nLinux\n\n### Operating System Details\n\n_No response_\n\n### SQLModel Version\n\n0.0.6\n\n### Python Version\n\n3.10\n\n### Additional Context\n\nalembic 1.8.1\r\nsqlalchemy 1.4.35","author":{"url":"https://github.com/kristjanvalur","@type":"Person","name":"kristjanvalur"},"datePublished":"2022-11-08T14:14:06Z","url":"https://github.com/fastapi/sqlmodel/issues/491"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:7de49cea-caea-1d10-ee4f-a42f29978319
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE6F4:74373:4368BF:5B0F6B:6AFE20B5
html-safe-nonce6248e83b64cf6e470860ce242d1ba50ccaa567354f247bc9f95f0642cca7d1bd
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNkY0Ojc0MzczOjQzNjhCRjo1QjBGNkI6NkFGRTIwQjUiLCJ2aXNpdG9yX2lkIjoiMTY2ODk0NjY0MDAxMTkyNzczMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb45cec242dda3179842015c0d9c106e9751165c6bf7eb7b366d7e352b1eae29c
hovercard-subject-tagissue:1440286340
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/fastapi/sqlmodel/491/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f05f2b72310ffb3943c8ad2859b38d96b2521e58afcf934f1b2def1449a092f8/fastapi/sqlmodel/issues/491
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f05f2b72310ffb3943c8ad2859b38d96b2521e58afcf934f1b2def1449a092f8/fastapi/sqlmodel/issues/491
og:image:altFirst Check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't find it. I searched the SQLModel documentation, with the integrated search. I...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamekristjanvalur
hostnamegithub.com
expected-hostnamegithub.com
Nonee973f9ec3625f3faad8cac5c2d11a214b7ffe45f3cc563b828459b290ecaeed3
turbo-cache-controlno-preview
go-importgithub.com/fastapi/sqlmodel git https://github.com/fastapi/sqlmodel.git
octolytics-dimension-user_id156354296
octolytics-dimension-user_loginfastapi
octolytics-dimension-repository_id399495186
octolytics-dimension-repository_nwofastapi/sqlmodel
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id399495186
octolytics-dimension-repository_network_root_nwofastapi/sqlmodel
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release1f92c367595b9190e0be293896078033886b1913
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/fastapi/sqlmodel/issues/491#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Ffastapi%2Fsqlmodel%2Fissues%2F491
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Ffastapi%2Fsqlmodel%2Fissues%2F491
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=fastapi%2Fsqlmodel
Reloadhttps://github.com/fastapi/sqlmodel/issues/491
Reloadhttps://github.com/fastapi/sqlmodel/issues/491
Reloadhttps://github.com/fastapi/sqlmodel/issues/491
fastapihttps://github.com/fastapi
sqlmodelhttps://github.com/fastapi/sqlmodel
Codehttps://github.com/fastapi/sqlmodel
Issues1 (1)https://github.com/fastapi/sqlmodel/issues
Pull requests53 (53)https://github.com/fastapi/sqlmodel/pulls
Discussionshttps://github.com/fastapi/sqlmodel/discussions
Actionshttps://github.com/fastapi/sqlmodel/actions
Projectshttps://github.com/fastapi/sqlmodel/projects
Security and qualityhttps://github.com/fastapi/sqlmodel/security
Insightshttps://github.com/fastapi/sqlmodel/insights
https://github.com/fastapi/sqlmodel
New issuehttps://github.com/login?return_to=https://github.com/fastapi/sqlmodel/issues/491
New issuehttps://github.com/login?return_to=https://github.com/fastapi/sqlmodel/issues/491
Migrations with UUID and server_default fail.https://github.com/fastapi/sqlmodel/issues/491#top
investigatehttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22investigate%22
questionFurther information is requestedhttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22question%22
waitinghttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22waiting%22
https://github.com/kristjanvalur
kristjanvalurhttps://github.com/kristjanvalur
on Nov 8, 2022https://github.com/fastapi/sqlmodel/issues/491#issue-1440286340
Pydantichttps://github.com/samuelcolvin/pydantic
SQLAlchemyhttps://github.com/sqlalchemy/sqlalchemy
kristjanvalurhttps://github.com/kristjanvalur
questionFurther information is requestedhttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22question%22
on Nov 8, 2022https://github.com/fastapi/sqlmodel/issues/491#event-7763243631
tiangolohttps://github.com/tiangolo
investigatehttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22investigate%22
on Nov 11, 2022https://github.com/fastapi/sqlmodel/issues/491#event-7790204467
https://github.com/notypecheck
notypecheckhttps://github.com/notypecheck
on Nov 12, 2022https://github.com/fastapi/sqlmodel/issues/491#issuecomment-1312515221
https://github.com/yinziyan1206
yinziyan1206https://github.com/yinziyan1206
on Nov 14, 2022https://github.com/fastapi/sqlmodel/issues/491#issuecomment-1312955773
https://github.com/jpmckinney
jpmckinneyhttps://github.com/jpmckinney
on Aug 19, 2024https://github.com/fastapi/sqlmodel/issues/491#issuecomment-2297170346
https://github.com/YuriiMotov
YuriiMotovhttps://github.com/YuriiMotov
on Aug 26, 2025https://github.com/fastapi/sqlmodel/issues/491#issuecomment-3222893801
YuriiMotovhttps://github.com/YuriiMotov
waitinghttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22waiting%22
on Aug 26, 2025https://github.com/fastapi/sqlmodel/issues/491#event-19337088509
https://github.com/kristjanvalur
kristjanvalurhttps://github.com/kristjanvalur
on Aug 26, 2025https://github.com/fastapi/sqlmodel/issues/491#issuecomment-3223299478
kristjanvalurhttps://github.com/kristjanvalur
completedhttps://github.com/fastapi/sqlmodel/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Aug 26, 2025https://github.com/fastapi/sqlmodel/issues/491#event-19339484152
Sign up for freehttps://github.com/signup?return_to=https://github.com/fastapi/sqlmodel/issues/491
Sign in to commenthttps://github.com/login?return_to=https://github.com/fastapi/sqlmodel/issues/491
investigatehttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22investigate%22
questionFurther information is requestedhttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22question%22
waitinghttps://github.com/fastapi/sqlmodel/issues?q=state%3Aopen%20label%3A%22waiting%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.