Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create and updateRecord throw exception when there is generated column #1838

Open
pmtsoftware opened this issue Oct 6, 2023 · 0 comments
Open
Labels

Comments

@pmtsoftware
Copy link

pmtsoftware commented Oct 6, 2023

Let's consider blog posts as an example. In order to enable full text search on post body new column bodyIndexCol is created :

ALTER TABLE posts
    ADD COLUMN body_index_col tsvector
               GENERATED ALWAYS AS (to_tsvector('english', coalesce(body, ''))) STORED;
               
-- creating GIN index skipped due to irrelevance

After migration the Post constructor is extended with new bodyIndexCol as expected:

data Post'  = Post 
    { id :: (Id' "posts")
    , title :: Text
    , body :: Text
    , bodyIndexCol :: (Maybe TSVector) 
    , meta :: MetaBag
    } deriving (Eq, Show)

Now either creating new or updating any existing post crashes with error:

ConversionFailed 
  { errSQLType = "3 values: [\"uuid\",\"text\",\"text\"]"
  , errSQLTableOid = Nothing
  , errSQLField = ""
  , errHaskellType = "at least 4 slots in target type"
  , errMessage = "mismatch between number of columns to convert and number in target type"
  }

If we look into generated CanCreate instance for Post type:

instance CanCreate Post where
    ...
    create model = do
        List.head <$> sqlQuery "INSERT INTO posts (id, title, body) VALUES (?, ?, ?) RETURNING id, title, body" ((fieldWithDefault #id model, model.title, model.body))
    ...

we see that RETURNING clause of generated SQL insert statement contains only writeable fields (without new bodyIndexCol column) so Post constructor cannot be satisfied and leads to above error.
From db level new post was created indeed. Similar problem with createMany and update.

@mpscholten mpscholten added the bug label Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants