Description

When adding FK to existing SQLite table, table is rewritten with FK added but existing indexes are not recreated after rewrite.

To reproduce:

(1) create table with index:

CREATE TABLE `items` (`id` integer NOT NULL, `name` varchar(100) NOT NULL,`user_id` integer,PRIMARY KEY (`id`))

CREATE INDEX `idx_items_name` ON `items`(`name`)

(2) add FK to existing table created in (1) with Migrator().CreateConstraint() :

# Adding FK from items table to users table with Migrator().CreateConstraint() makes GORM to recreate table with FK...

CREATE TABLE `items__temp` (`id` integer NOT NULL,`name` varchar(100) NOT NULL,`user_id` integer, PRIMARY KEY (`id`),CONSTRAINT `fk_users_items` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`))

INSERT INTO `items__temp`(`id`,`name`,`user_id`) SELECT `id`,`name`,`user_id` FROM `items`

DROP TABLE `items`

ALTER TABLE `items__temp` RENAME TO `items`

# ...but existing indexes (like idx_items_name above) are not recreated in new table.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: pboguslawski

No such problem with recreating existing FK when other FK is added to the same SQlite table; so rewriting SQlite table on adding FK preservers existing FK but not existing indexes.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

If you are using AutoMigrate, you need to add tag gorm:"index" to ensure it will not lose.

Comment From: pboguslawski

If you are using AutoMigrate, you need to add tag gorm:"index" to ensure it will not lose.

Problem was noticed when adding FK with Migrator().CreateConstraint(). AutoMigrate was not checked.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

Patch Constraint is not currently supported https://github.com/go-gorm/gorm/issues/5559#issuecomment-1200718658

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking