SQL statement generation
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.
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)]
I’ll keep it for a few days for the team to see. Thank you for your efforts. I’ll close it later.
Yes you're right, auto_increment is set implicitly. Thank you for posting the solution.