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

Getting DB::ColumnTypeMismatchError for Int32 columns in CockroachDB #919

Closed
akadusei opened this issue Jan 16, 2023 · 3 comments · Fixed by #920
Closed

Getting DB::ColumnTypeMismatchError for Int32 columns in CockroachDB #919

akadusei opened this issue Jan 16, 2023 · 3 comments · Fixed by #920

Comments

@akadusei
Copy link
Contributor

I have an app that I'm currently testing on CockroachDB, and encounter this error:

In PG::ResultSet#read the column one returned a Int64 but a Int32 was expected. (DB::ColumnTypeMismatchError)
    from lib/db/src/db/result_set.cr:89:9 in 'read'
    from lib/db/src/db/query_methods.cr:202:9 in 'query_one?:as:args'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:as:args:queryable'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:args:queryable:as'
    from lib/avram/src/avram/queryable.cr:224:17 in 'any?'
    # ...

The problem appears to be that in CockroachDB (v22.2), int is an alias for int8. In PostgreSQL, int refers to int4. So for an Int32 column in Avram, CockroachDB is creating an int8 column whereas crystal-pg expects to read an int4.

Not tested, but this may be solved by changing the Avram::Migrator::Columns::Int32Column#column_type to int4:

While at it, we may as well change Avram::Migrator::Columns::PrimaryKeys::Int32PrimaryKey#column_type to serial4:

These changes should make it explicit to both DB engines what the column type is. This should not be a breaking change.

@jwoertink
Copy link
Member

Oh nice. I wasn't aware of those types. Any idea how far back of a Postgres version that works on? At least to v12?

@akadusei
Copy link
Contributor Author

Any idea how far back of a Postgres version that works on? At least to v12?

From the PostgreSQL docs, as far back as v7.2: https://www.postgresql.org/docs/7.2/datatype.html

@jwoertink
Copy link
Member

nice! Yeah, in that case, it should be fine, I think.

akadusei added a commit to akadusei/avram that referenced this issue Jan 16, 2023
Fixes luckyframework#919

```
In PG::ResultSet#read the column one returned a Int64 but a Int32 was expected. (DB::ColumnTypeMismatchError)
    from lib/db/src/db/result_set.cr:89:9 in 'read'
    from lib/db/src/db/query_methods.cr:202:9 in 'query_one?:as:args'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:as:args:queryable'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:args:queryable:as'
    from lib/avram/src/avram/queryable.cr:224:17 in 'any?'
    # ...
```
jwoertink pushed a commit that referenced this issue Jan 16, 2023
…920)

Fixes #919

```
In PG::ResultSet#read the column one returned a Int64 but a Int32 was expected. (DB::ColumnTypeMismatchError)
    from lib/db/src/db/result_set.cr:89:9 in 'read'
    from lib/db/src/db/query_methods.cr:202:9 in 'query_one?:as:args'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:as:args:queryable'
    from lib/avram/src/avram/database.cr:88:3 in 'query_one?:args:queryable:as'
    from lib/avram/src/avram/queryable.cr:224:17 in 'any?'
    # ...
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants