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

SQL statement generation

Open Hell0wor1d opened this issue 1 year ago • 3 comments

Hi there,

Why does the SQL statement generated by the following code automatically include AUTOINCREMENT? My field is a String type primary key, so why is this property being generated automatically? Is this a bug or was it intentionally designed this way? How should I adjust it? Otherwise, the table creation fails with the following exception:

DatabaseError(Exec(SqlxError(Database(SqliteError { code: 1, message: "AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY" }))))

"CREATE TABLE IF NOT EXISTS "cache_index" ( "key" text NOT NULL PRIMARY KEY AUTOINCREMENT, "value" text NOT NULL, "expire_at" bigint )"
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cache_index")]
pub struct Model {
    #[sea_orm(primary_key, column_type = "Text")]
    pub key: String,
    #[sea_orm(column_type = "Text")]
    pub value: String,
    #[sea_orm(column_type = "BigInteger", nullable)]
    pub expire_at: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

Thanks for your help.

Hell0wor1d avatar Feb 18 '25 08:02 Hell0wor1d

Okay, I found a solution. I need to explicitly set auto_increment = false. Of course, it would be better if the logic could be optimized so that auto-increment is not enabled when the field type is not an integer.

    #[sea_orm(primary_key, column_type = "Text", auto_increment = false)]

Hell0wor1d avatar Feb 18 '25 08:02 Hell0wor1d

I’ll keep it for a few days for the team to see. Thank you for your efforts. I’ll close it later.

Hell0wor1d avatar Feb 18 '25 08:02 Hell0wor1d

Yes you're right, auto_increment is set implicitly. Thank you for posting the solution.

tyt2y3 avatar Feb 18 '25 09:02 tyt2y3