yesodweb / yesodweb/persistent

MySQL migration doesn't do Foreign key right

Open
#911 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

I get the following error:

    Migrating: CREATe TABLE `person_x`( PRIMARY KEY (`username`),`username` TEXT CHARACTER SET utf8 NOT NULL,`name` TEXT CHARACTER SET utf8 NOT NULL,`age` BIGINT NULL)
    CREATe TABLE `person_x`( PRIMARY KEY (`username`),`username` TEXT CHARACTER SET utf8 NOT NULL,`name` TEXT CHARACTER SET utf8 NOT NULL,`age` BIGINT NULL);
    ALTER TABLE `person_x` ADD CONSTRAINT `unique_name` UNIQUE(`name`(200));
    CREATe TABLE `blog_post_x`(`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,`title` TEXT CHARACTER SET utf8 NOT NULL,`author_id` TEXT CHARACTER SET utf8 NOT NULL REFERENCES `person_x`);
    ALTER TABLE `blog_post_x` ADD CONSTRAINT `blog_post_x_author_id_fkey` FOREIGN KEY(`author_id`) REFERENCES `person_x`(`username`);
    CREATe TABLE `alias_x`(`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,`person` TEXT CHARACTER SET utf8 NOT NULL,`alias` TEXT CHARACTER SET utf8 NOT NULL);
    test: ConnectionError {errFunction = "query", errNumber = 1170, errMessage = "BLOB/TEXT column 'username' used in key specification without a key length"}

from the following migration:

share [mkPersist sqlSettings, mkMigrate "migrateAll", mkDeleteCascade sqlSettings] [persistLowerCase|
PersonX
    username String
    name String
    age Int Maybe
    UniqueName name
    Primary username
    deriving Show
BlogPostX
    title String
    authorId PersonXId
    deriving Show
AliasX
    person String
    alias String
    -- TODO: Uncomment when we can implement non-primary key foreign key
    -- references.
    -- Foreign Person fkperson person name
    deriving Show
|]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the migration from the issue's PersonX, BlogPostX, and AliasX definitions against MySQL, then inspect the generated CREATE TABLE and ALTER TABLE statements around the author_id foreign key. Done means the migration completes without MySQL error 1170 while preserving the intended foreign-key relationship.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, mysql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.