If the table in postgres is as follows:
CREATE TABLE user (
id integer PRIMARY KEY,
email text UNIQUE,
height_cm numeric,
height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);
And the sqlalchemy model is:
from sqlalchemy.ext.declarative import declarative_base
class User(declarative_base()):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
email = Column(String, unique=True, index=True, nullable=False)
height_cm = Column(Numeric)
height_in = Column(Numeric)
How to correctly deal with the generated height_in column is sqlalchemy?
The idea is to only insert id, email and height_cm using sqlalchemy however by specifying the column height_in sqlalchemy automatically inserts NULL into height_in when inserting a row into the table - and postgres then errors out as this is not allowed.