sqlalchemy: Create Generated Column in MariaDB cannot specify null
Discussed in https://github.com/sqlalchemy/sqlalchemy/discussions/10055
<div type='discussions-op-text'>Originally posted by iamrinshibuya July 3, 2023 Hello, when using Computed Columns, the following code works without any changes on PostgreSQL, but it fails in MariaDB.
import asyncio
from sqlalchemy import Computed, text
from sqlalchemy.orm import (
Mapped,
DeclarativeBase,
mapped_column,
)
from sqlalchemy.ext.asyncio import create_async_engine
# works with this
# engine = create_async_engine('postgresql+psycopg://...')
# does not work with this
engine = create_async_engine('mariadb+asyncmy://...')
class Base(DeclarativeBase):
pass
class Sqaure(Base):
__tablename__ = 'square'
id: Mapped[int] = mapped_column(primary_key=True)
side: Mapped[int]
area: Mapped[int] = mapped_column(Computed(text('4 * side')), index=True)
async def main():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
asyncio.run(main())
sqlalchemy.exc.ProgrammingError: (asyncmy.errors.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NOT NULL, \n\tPRIMARY KEY (id)\n)' at line 4")
[SQL:
CREATE TABLE square (
id INTEGER NOT NULL AUTO_INCREMENT,
side INTEGER NOT NULL,
area INTEGER GENERATED ALWAYS AS (4 * side) NOT NULL,
PRIMARY KEY (id)
)
]
(Background on this error at: https://sqlalche.me/e/20/f405)
What should I do so that the table gets created in MariaDB?
Making the column nullable (area: Mapped[int | None]) seems to fix this, but I have the following concerns
- I find it counterproductive in my case, the generated column always has a value.
- In PostgreSQL this is a “non-nullable” / required column and I’d like to not change the behavior just to accommodate MariaDB (the code has to support both dialects)</div>
The docs of mariadb seem to indicate that the null clause is not allowed with generated always https://mariadb.com/kb/en/generated-columns/
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 16 (12 by maintainers)
Commits related to this issue
- fixes: #10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-support adde... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fixes: #10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-support adde... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fixes: #10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-support adde... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fixes: sqlalchemy#10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-sup... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fixes: sqlalchemy#10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-sup... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fixes: sqlalchemy#10056, handle nullable for generated column in mariadb mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-sup... — committed to indiVar0508/sqlalchemy by indiVar0508 9 months ago
- fix(#10056): keep nullable as true for mariadb generated columns mariaDB does not support setting NOT NULL for generated column ref: https://mariadb.com/kb/en/generated-columns/#statement-support ad... — committed to sqlalchemy/sqlalchemy by indiVar0508 8 months ago
- Merge "fix(#10056): keep nullable as true for mariadb generated columns" into main — committed to sqlalchemy/sqlalchemy by zzzeek 8 months ago
computed is actually since 1.3. I would guess this issue is also on 1.4.