AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
This error occurs when you try to use AUTOINCREMENT on a column that isn't INTEGER PRIMARY KEY. Learn about SQLite's AUTOINCREMENT requirements.
The AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY error means you're trying to use AUTOINCREMENT incorrectly.
Understanding the Error
Error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
AUTOINCREMENT has strict requirements in SQLite.
Common Causes
1. Wrong Data Type
SQL
2. Not Primary Key
SQL
3. Composite Primary Key
SQL
How to Fix It
Solution 1: Use INTEGER PRIMARY KEY
SQL
Solution 2: Skip AUTOINCREMENT
For most cases, you don't need AUTOINCREMENT:
SQL
Solution 3: Use Trigger for Other Types
SQL
Solution 4: Generate IDs in Application
JAVASCRIPT
INTEGER PRIMARY KEY vs AUTOINCREMENT
Both auto-generate IDs, but differently:
SQL
When to Use AUTOINCREMENT
Use it when:
- IDs must never be reused (audit trails, references)
- External systems cache IDs
Skip it when:
- Performance is critical
- ID reuse is acceptable
- Table is frequently emptied
Best Practices
- Use INTEGER PRIMARY KEY for most auto-ID needs
- Add AUTOINCREMENT only when IDs can't be reused
- Use application-generated IDs for distributed systems
- Document your ID strategy for the team