Migration with foreign key failing with two separate calls to runMigration #152

Open
bergmark opened this Issue Sep 12, 2013 · 9 comments

Comments

Projects
None yet
4 participants
Contributor

bergmark commented Sep 12, 2013

Description copied from the google group

I have a pretty simple test case with two models (in separate modules) with a foreign key constraint, https://gist.github.com/bergmark/6539373

I make a call to runMigration for each module and then get the error "Table not found: A" on the second migration. If i put both models in the same persist block this problem doesn't occur. This is with the latest hackage versions of persistent, persistent-template, persistent-sqlite (and persistent-postgresql).

Contributor

bergmark commented Oct 14, 2013

I checked this again using the same gist as in #162, https://gist.github.com/bergmark/6935922

One step in the right direction. Running the initial migration works, re-running it does not:

$ test
Migrating: CREATe TABLE "foo"("id" SERIAL PRIMARY KEY UNIQUE)
NOTICE:  CREATE TABLE will create implicit sequence "foo_id_seq" for serial column "foo.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo"
Migrating: CREATe TABLE "bar"("id" SERIAL PRIMARY KEY UNIQUE,"foo_id" INT8 NOT NULL)
NOTICE:  CREATE TABLE will create implicit sequence "bar_id_seq" for serial column "bar.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "bar_pkey" for table "bar"

$ test
Migrating: CREATe TABLE "foo"("id" SERIAL PRIMARY KEY UNIQUE)
NOTICE:  CREATE TABLE will create implicit sequence "foo_id_seq1" for serial column "foo.id"
test: SqlError {sqlState = "42P07", sqlExecStatus = FatalError, sqlErrorMsg = "relation \"foo\" already exists", sqlErrorDetail = "", sqlErrorHint = ""}
Owner

snoyberg commented Oct 15, 2013

This is caused by a totally different issue: PostgreSQL migrations for tables with no columns. We test for table existence right now by looking for columns in the table.

Owner

snoyberg commented Oct 15, 2013

@bergmark Should be fixed with f4d4bf3, can you confirm?

Contributor

bergmark commented Oct 15, 2013

It works, thanks!

The only thing that remains now is, as we discussed, that the foreign keys aren't added when the tables are defined in different blocks.

gregwebs added the Postgres label Aug 4, 2014

I came across this thread when looking for a solution to a similar foreign key problem as @bergmark. Has the issue of foreign keys not being added when tables are defined in different modules been fixed, or is there a work around? I, too, would like to have foreign key constraints active even when the linked entities are in separate modules. Even if I have to enter a command manually in the block to get the foreign keys to work, that would be much better than not having any FK constraints or needing to define all of the tables in the same block/module.

Owner

snoyberg commented Sep 30, 2014

I don't believe we've made any changes on this front. Is that right @gregwebs?

Owner

gregwebs commented Sep 30, 2014

I don't touch migrations :)

Owner

gregwebs commented May 15, 2015

I did cleanup some migrations (so try the latest version), but I have not tested cross-module behavior.

Owner

snoyberg commented Jul 19, 2015

Closing out old issues, please reopen if still relevant

snoyberg closed this Jul 19, 2015

gregwebs reopened this Jul 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment