Skip to content

Conversation

@smzgl
Copy link
Contributor

@smzgl smzgl commented Dec 26, 2025

当不同数据库存在相同表名和相同字段名, 并且该字段存在约束时, 例如字段类型是JSON, 会出现字段叠加. 导致访问数据库时, 出现数组越界.

smzgl and others added 2 commits August 28, 2025 03:22
当不同数据库存在相同表名和相同字段名,  并且该字段存在约束时, 例如字段类型是JSON,  会出现字段叠加. 导致访问数据库时, 出现数组越界.
@hailaz
Copy link
Contributor

hailaz commented Dec 27, 2025

希望能举个具体的例子,方便写个单元测试验证

@smzgl
Copy link
Contributor Author

smzgl commented Dec 27, 2025

希望能举个具体的例子,方便写个单元测试验证

同一个mysql 或者 mariadb 实例中, 先后创建数据库 db_a和db_b, 在db_a和db_b创建同名表 t, db_a.t 有8个字段, db_b.t有10个字段. 最后一个字段的字段名同名. 并且该字段存在约束(比如是一个JSON类型字段), 当使用从db_a.t 或者db_b.t 读取数据的时候可能会触发越界的panic. <大概是这个case, 时间有点久了>

具体原因:

  1. TableFields 是使用 tableFieldsSqlByMariadb变量的sql, 获取字段列表的. 该SQL从 information_schema 读取表结构的时候, 并没有根据schema的名称进行过滤, 导致读取到所有schema同名表的字段.
  2. 返回值result类型是map, key为字段名. 在遍历步骤1返回结果的时候, 后面的同名字段会覆盖前一个的. 该字段信息包括了一个重要字段Index. 也就是字段在表中的次序.
  3. 当进行Scan的时候, 会直接使用Index作为字段列索引进行访问. 当两个表结构不同, 字段数量不一样的时候, 会发生数组越界异常.

我看现在版本mariadb_table_fields.go 也会有此问题, 也应该按照该方法修正

@hailaz
Copy link
Contributor

hailaz commented Dec 27, 2025

希望能举个具体的例子,方便写个单元测试验证

同一个mysql 或者 mariadb 实例中, 先后创建数据库 db_a和db_b, 在db_a和db_b创建同名表 t, db_a.t 有8个字段, db_b.t有10个字段. 最后一个字段的字段名同名. 并且该字段存在约束(比如是一个JSON类型字段), 当使用从db_a.t 或者db_b.t 读取数据的时候可能会触发越界的panic. <大概是这个case, 时间有点久了>

具体原因:

  1. TableFields 是使用 tableFieldsSqlByMariadb变量的sql, 获取字段列表的. 该SQL从 information_schema 读取表结构的时候, 并没有根据schema的名称进行过滤, 导致读取到所有schema同名表的字段.
  2. 返回值result类型是map, key为字段名. 在遍历步骤1返回结果的时候, 后面的同名字段会覆盖前一个的. 该字段信息包括了一个重要字段Index. 也就是字段在表中的次序.
  3. 当进行Scan的时候, 会直接使用Index作为字段列索引进行访问. 当两个表结构不同, 字段数量不一样的时候, 会发生数组越界异常.

我看现在版本mariadb_table_fields.go 也会有此问题, 也应该按照该方法修正

好的,我后续加个单元测试验证后合并

@hailaz hailaz requested a review from Copilot January 7, 2026 08:07
@hailaz hailaz changed the title fix:修复mysql不同数据库下相同表名相同字段名出现字段叠加的问题 fix(contrib/drivers): resolve field duplication issue when same table/column names exist across different MySQL/MariaDB databases Jan 7, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug where querying table fields in MySQL/MariaDB drivers returns incorrect field indexes when multiple database schemas contain tables with the same name and JSON fields. The issue stems from the LEFT JOIN with CHECK_CONSTRAINTS table lacking a schema filter, causing rows from different schemas to be incorrectly joined and producing duplicate field entries.

Key Changes:

  • Added schema filtering (TABLE_SCHEMA = CONSTRAINT_SCHEMA) to the CHECK_CONSTRAINTS JOIN in both MySQL and MariaDB drivers
  • Added regression tests for multi-schema scenarios with identical table names
  • Included development tooling (Makefile docker target and docker-services.sh script) for easier local testing

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
contrib/drivers/mysql/mysql_table_fields.go Added TABLE_SCHEMA filter to CHECK_CONSTRAINTS JOIN to prevent cross-schema constraint matching
contrib/drivers/mariadb/mariadb_table_fields.go Added TABLE_SCHEMA filter to CHECK_CONSTRAINTS JOIN to prevent cross-schema constraint matching
contrib/drivers/mysql/mysql_z_unit_issue_test.go Added test verifying field indexes remain valid when identical tables exist in multiple schemas
contrib/drivers/mariadb/mariadb_unit_model_test.go Added comprehensive test with JSON fields demonstrating the bug fix for multi-schema scenarios
contrib/drivers/mariadb/mariadb_unit_init_test.go Initialized db2 variable for multi-schema testing
contrib/drivers/mariadb/mariadb.go Added New method implementation for driver instantiation
Makefile Added docker target for managing test services
.github/workflows/scripts/docker-services.sh Added comprehensive script for managing Docker-based test dependencies

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gqcn
Copy link
Member

gqcn commented Jan 7, 2026

@smzgl LGTM,请解决Copilot的问题后可以合并。

@hailaz hailaz merged commit c577812 into gogf:master Jan 7, 2026
18 checks passed
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.

3 participants