sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

Cannot set column to nullable

Open ou-bing opened this issue 1 year ago • 4 comments

Description

.col(date_time(UrlMapping::ExpiredAt).null())

When I execute ·sea-orm-cli migrate-v·, the console prompts:

2024-10-16T14:13:34.698031Z  INFO sqlx::query: summary="CREATE TABLE IF NOT …" db.statement="\n\nCREATE TABLE IF NOT EXISTS \"url_mapping\" (\n  \"id\" serial NOT NULL PRIMARY KEY,\n  \"source_url\" char(255) NOT NULL NOT NULL,\n  \"target_url_protocol\" char(255) NOT NULL NOT NULL,\n  \"target_url_domain\" char(255) NOT NULL NOT NULL,\n  \"target_url_path\" char(255) NOT NULL NOT NULL,\n  \"created_at\" timestamp without time zone NOT NULL NOT NULL,\n  \"expired_at\" timestamp without time zone NOT NULL NULL,\n  \"deleted_at\" timestamp without time zone NOT NULL\n)\n" rows_affected=0 rows_returned=0 elapsed=1.81815ms elapsed_secs=0.00181815
Execution Error: error returned from database: conflicting NULL/NOT NULL declarations for column "expired_at" of table "url_mapping"
Fail to run migration

NOT NULL NULL appears simultaneously in the ddl statement

Reproducible Example

https://github.com/ou-bing/seaorm_null_err

Versions

sea-orm-cli 1.1.0 sea-orm-migration 1.1.0 rustc 1.81.0

postgres 17

ou-bing avatar Oct 16 '24 14:10 ou-bing

When using SQLite, it does not report errors, but it also does not take effect

ou-bing avatar Oct 16 '24 15:10 ou-bing

Using .col(date_time_null(UrlMapping::ExpiredAt)) is the correct usage. The .null() method is highly misleading.

ou-bing avatar Oct 17 '24 13:10 ou-bing

I've also encountered this issue. Have you found a version without this bug? I'm using PostgreSQL 16 and sea-orm-migration 1.1.7. All data fields default to NOT NULL, and if I force them to NULL, conflicts occur.

yenharvey avatar Mar 19 '25 14:03 yenharvey

I understand your point now, thank you so much! This behavior is quite counterintuitive and opposite to database conventions. By default, fields are not_null, and then some fields have xx_null or xx_nulq, and you can also use .null(). I can't imagine how long it would take to find the problem without auto-completion.

yenharvey avatar Mar 19 '25 14:03 yenharvey