Repository navigation
fix: mySQL does not support default false - #1393
Conversation
WalkthroughThis change removes the explicit GORM default value tags ( Changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
| CompletionTokens int `json:"completion_tokens" gorm:"default:0"` | ||
| UseTime int `json:"use_time" gorm:"default:0"` | ||
| IsStream bool `json:"is_stream" gorm:"default:false"` | ||
| IsStream bool `json:"is_stream"` |
There was a problem hiding this comment.
💡 Verification agent
❓ Verification inconclusive
Specify NOT NULL to prevent NULL → bool scan failures
After dropping gorm:"default:false", GORM will no longer emit a DEFAULT clause.
However, nothing now guarantees the column stays NOT NULL.
If the column slips to NULL, any SELECT into a Go bool (non-pointer) will panic with “sql: Scan error on column … converting NULL to bool”.
Adding an explicit not null constraint preserves the previous safety without re-introducing the problematic default value.
-IsStream bool `json:"is_stream"`
+IsStream bool `json:"is_stream" gorm:"not null"`Add NOT NULL constraint to IsStream field
After removing gorm:"default:false", the column can become nullable, leading to “sql: Scan error on column … converting NULL to bool” when scanning into a Go bool. Enforce NOT NULL at the schema level to retain the previous safety without reintroducing a default value.
• File: model/log.go
• Line: 30
-IsStream bool `json:"is_stream"`
+IsStream bool `json:"is_stream" gorm:"not null"`📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| IsStream bool `json:"is_stream"` | |
| // in model/log.go, around line 30 | |
| IsStream bool `json:"is_stream" gorm:"not null"` |
🤖 Prompt for AI Agents
In model/log.go at line 30, the IsStream field lacks a NOT NULL constraint after
removing the default tag, which can cause SQL scan errors when NULL values are
encountered. Add a gorm tag to enforce NOT NULL on the IsStream column to
prevent it from being nullable, ensuring safe scanning into the Go bool type
without reintroducing a default value.
| UnlimitedQuota bool `json:"unlimited_quota"` | ||
| ModelLimitsEnabled bool `json:"model_limits_enabled"` |
There was a problem hiding this comment.
💡 Verification agent
🧩 Analysis chain
Retain NOT NULL semantics for boolean flags
Removing gorm:"default:false" fixes the MySQL migration loop, but the columns still need to disallow NULL because:
- Your code reads these fields into plain
boolvalues (e.g.token.UnlimitedQuota) – aNULLcoming from DB will cause a scan error. - Business logic relies on deterministic
falsewhen the column is not explicitly set.
Safest tweak:
-UnlimitedQuota bool `json:"unlimited_quota"`
-ModelLimitsEnabled bool `json:"model_limits_enabled"`
+UnlimitedQuota bool `json:"unlimited_quota" gorm:"not null"`
+ModelLimitsEnabled bool `json:"model_limits_enabled" gorm:"not null"`This keeps the columns TINYINT(1) NOT NULL (MySQL) / BOOLEAN NOT NULL (PostgreSQL) while avoiding the DEFAULT clause that caused the original issue.
🏁 Script executed:
#!/bin/bash
set -e
echo "Searching for Token struct definition in model/token.go..."
rg -n "type Token" model/token.go || true
echo -e "\nPreview of the first 200 lines of model/token.go:"
sed -n '1,200p' model/token.go || trueLength of output: 6495
🏁 Script executed:
#!/bin/bash
set -e
echo "Searching for AutoMigrate references..."
rg -n "AutoMigrate" -C2 .
echo -e "\nSearching for migrations directory..."
fd -t d -I migrate || true
echo -e "\nSearching for SQL migration files..."
fd -t f -I .sql || true
echo -e "\nSearching for GORM migrate calls (Migrate, Migrator)..."
rg -n "Migrate" -C2 .Length of output: 4774
Retain NOT NULL semantics for boolean flags
Your code reads these fields into plain bool values and relies on deterministic false when unset. Without gorm:"not null", the generated columns allow NULL, which will cause scan errors and unpredictable defaults.
Please update model/token.go:
@@ type Token struct {
- UnlimitedQuota bool `json:"unlimited_quota"`
- ModelLimitsEnabled bool `json:"model_limits_enabled"`
+ UnlimitedQuota bool `json:"unlimited_quota" gorm:"not null"`
+ ModelLimitsEnabled bool `json:"model_limits_enabled" gorm:"not null"`This ensures both columns are created as TINYINT(1) NOT NULL (MySQL) or BOOLEAN NOT NULL (PostgreSQL) while avoiding the DEFAULT clause that was triggering the migration loop.
🤖 Prompt for AI Agents
In model/token.go at lines 23 to 24, the boolean fields UnlimitedQuota and
ModelLimitsEnabled lack the gorm:"not null" tag, which can cause the database
columns to allow NULL values leading to scan errors and unpredictable defaults.
Add the struct tag `gorm:"not null"` to both fields to enforce NOT NULL
constraints without adding a DEFAULT clause, ensuring consistent and safe
boolean handling in the database.
…ault-false fix: mySQL does not support default false
…ault-false fix: mySQL does not support default false
…ault-false fix: mySQL does not support default false
mysql的bool是tinyint false是0
default:false会造成Automigrate每次都执行alter更新default但每次都修改不成功
去掉default, 不影响功能, 因为go的bool默认就是false, 也能同时兼容mysql和postgre数据库
Summary by CodeRabbit