[MDEV-21202] JSON alias causes constraints to be skipped Created: 2019-12-03  Updated: 2019-12-17  Resolved: 2019-12-17

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Alter Table, Data Definition - Create Table, Data types
Affects Version/s: 10.4.10
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Erik Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

debian buster
10.4.10-MariaDB-1:10.4.10+maria~buster-log - mariadb.org binary distribution


Attachments: PNG File screenshot-1.png    

 Description   

This command successfully creates the table:

create table `test` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `additionalData` json null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'

This command successfully creates the test_user_id_foreign key, but it does not create the constraint:

alter table `test` add constraint `test_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade

If I use LONGTEXT instead of JSON as data type for the "additionalData" field, it works however.
I also tested it on a version 10.3.11 server, here it works both with json and longtext.

All queries run without throwing any exceptions



 Comments   
Comment by Elena Stepanova [ 2019-12-03 ]

Which constraint does it not create, and what works with LONGTEXT? Could you please paste the resulting table structure and point out what exactly the problem is?

Comment by Erik [ 2019-12-03 ]


The Index (marked blue) is created, but the constraint (marked red) is not.
if I use longtext, both the index and the constraint are created as expected

Comment by Alice Sherepa [ 2019-12-17 ]

I could not reproduce it:

MariaDB [test]> create table users (id bigint unsigned NOT NULL primary key)engine=innodb;
Query OK, 0 rows affected (0.025 sec)
 
MariaDB [test]> create table `test` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `additionalData` json null, `created_at` timestamp null, `updated_at` timestamp null) engine=innodb default character set utf8mb4 collate 'utf8mb4_unicode_ci';
Query OK, 0 rows affected (0.025 sec)
 
MariaDB [test]> alter table `test` add constraint `test_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade;
Query OK, 0 rows affected (0.127 sec)              
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table test;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `additionalData` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`additionalData`)),
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `test_user_id_foreign` (`user_id`),
  CONSTRAINT `test_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.001 sec)
 
MariaDB [test]> select version();
+-----------------+
| version()       |
+-----------------+
| 10.4.10-MariaDB |
+-----------------+
1 row in set (0.000 sec)

Comment by Erik [ 2019-12-17 ]

I too cannot reproduce it using the commandline, this might be a problem with phpmyadmin then, this can be closed.

Generated at Thu Feb 08 09:05:21 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.