Skip to content

fix: mySQL does not support default false - #1393

Merged
Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
feitianbubu:pr/fix-mysql-default-false
Jul 18, 2025
Merged

Calcium-Ion merged 1 commit into
QuantumNous:alphafrom
feitianbubu:pr/fix-mysql-default-false

Conversation

@feitianbubu

@feitianbubu feitianbubu commented Jul 18, 2025 •

Copy link
Copy Markdown
Member

mysql的bool是tinyint false是0
default:false会造成Automigrate每次都执行alter更新default但每次都修改不成功
去掉default, 不影响功能, 因为go的bool默认就是false, 也能同时兼容mysql和postgre数据库

Summary by CodeRabbit

  • Refactor
    • Updated database field configurations for certain settings, removing explicit default values for some options. No changes to app functionality or user interface.

@coderabbitai

coderabbitai Bot commented Jul 18, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This change removes the explicit GORM default value tags (gorm:"default:false") from several boolean fields in the Log and Token structs. The fields will now rely on Go's zero-value initialization rather than an explicit database default. No application logic or control flow is affected.

Changes

File(s) Change Summary
model/log.go Removed gorm:"default:false" tag from IsStream field in Log struct.
model/token.go Removed gorm:"default:false" tags from UnlimitedQuota and ModelLimitsEnabled fields in Token struct.

Poem

A tag once set, now gone away,
Booleans breathe in a simpler way.
No defaults whispered to the database ear,
Just Go’s own zero, crystal clear.
With lighter tags, our structs now gleam—
CodeRabbit hops in a streamlined dream! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e2a3d6 and e269b3b.

📒 Files selected for processing (2)
  • model/log.go (1 hunks)
  • model/token.go (1 hunks)

Comment thread model/log.go
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"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.

Suggested change
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.

Comment thread model/token.go
Comment on lines +23 to +24
UnlimitedQuota bool `json:"unlimited_quota"`
ModelLimitsEnabled bool `json:"model_limits_enabled"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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:

  1. Your code reads these fields into plain bool values (e.g. token.UnlimitedQuota) – a NULL coming from DB will cause a scan error.
  2. Business logic relies on deterministic false when 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 || true

Length 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.

@Calcium-Ion
Calcium-Ion merged commit 63a1904 into QuantumNous:alpha Jul 18, 2025
x22x22 pushed a commit to x22x22/new-api that referenced this pull request Apr 24, 2026
…ault-false

fix: mySQL does not support default false
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
…ault-false

fix: mySQL does not support default false
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
…ault-false

fix: mySQL does not support default false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants