Migration with SQL sequence is not applied in test environment












1















I have the current migration



class CreateSlugSequence < ActiveRecord::Migration[5.2]
def up
execute <<-SQL
CREATE SEQUENCE slug_sequence
SQL
end

def down
execute <<-SQL
DROP SEQUENCE slug_sequence
SQL
end
end


I run rails db:migrate



Enter rails c (development mode)



Run ActiveRecord::Base.connection.exec_query("select nextval('slug_sequence')")[0]['nextval']



And get the expected value



But if I enter rails c in test mode, for some reason, the sequence table does not exist



ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "slug_sequence" does not exist


I ran ActiveRecord::Migrator.current_version, to check what was the last migration applied and it returns the latest version.



Thanks for the help in advance.










share|improve this question























  • Perhaps RAILS_ENV=test rails db:migrate

    – David Aldridge
    Nov 19 '18 at 12:23











  • Yes I did it, that's why i checked the migration version present

    – Goamaral
    Nov 19 '18 at 12:42













  • Have you dropped test environment and recreate then reran?

    – Austio
    Nov 19 '18 at 12:46











  • @Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

    – Goamaral
    Nov 19 '18 at 12:53











  • @Austio Want the question rewards? Or should I answer my own question?

    – Goamaral
    Nov 19 '18 at 12:54
















1















I have the current migration



class CreateSlugSequence < ActiveRecord::Migration[5.2]
def up
execute <<-SQL
CREATE SEQUENCE slug_sequence
SQL
end

def down
execute <<-SQL
DROP SEQUENCE slug_sequence
SQL
end
end


I run rails db:migrate



Enter rails c (development mode)



Run ActiveRecord::Base.connection.exec_query("select nextval('slug_sequence')")[0]['nextval']



And get the expected value



But if I enter rails c in test mode, for some reason, the sequence table does not exist



ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "slug_sequence" does not exist


I ran ActiveRecord::Migrator.current_version, to check what was the last migration applied and it returns the latest version.



Thanks for the help in advance.










share|improve this question























  • Perhaps RAILS_ENV=test rails db:migrate

    – David Aldridge
    Nov 19 '18 at 12:23











  • Yes I did it, that's why i checked the migration version present

    – Goamaral
    Nov 19 '18 at 12:42













  • Have you dropped test environment and recreate then reran?

    – Austio
    Nov 19 '18 at 12:46











  • @Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

    – Goamaral
    Nov 19 '18 at 12:53











  • @Austio Want the question rewards? Or should I answer my own question?

    – Goamaral
    Nov 19 '18 at 12:54














1












1








1








I have the current migration



class CreateSlugSequence < ActiveRecord::Migration[5.2]
def up
execute <<-SQL
CREATE SEQUENCE slug_sequence
SQL
end

def down
execute <<-SQL
DROP SEQUENCE slug_sequence
SQL
end
end


I run rails db:migrate



Enter rails c (development mode)



Run ActiveRecord::Base.connection.exec_query("select nextval('slug_sequence')")[0]['nextval']



And get the expected value



But if I enter rails c in test mode, for some reason, the sequence table does not exist



ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "slug_sequence" does not exist


I ran ActiveRecord::Migrator.current_version, to check what was the last migration applied and it returns the latest version.



Thanks for the help in advance.










share|improve this question














I have the current migration



class CreateSlugSequence < ActiveRecord::Migration[5.2]
def up
execute <<-SQL
CREATE SEQUENCE slug_sequence
SQL
end

def down
execute <<-SQL
DROP SEQUENCE slug_sequence
SQL
end
end


I run rails db:migrate



Enter rails c (development mode)



Run ActiveRecord::Base.connection.exec_query("select nextval('slug_sequence')")[0]['nextval']



And get the expected value



But if I enter rails c in test mode, for some reason, the sequence table does not exist



ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "slug_sequence" does not exist


I ran ActiveRecord::Migrator.current_version, to check what was the last migration applied and it returns the latest version.



Thanks for the help in advance.







ruby-on-rails






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 11:56









GoamaralGoamaral

109212




109212













  • Perhaps RAILS_ENV=test rails db:migrate

    – David Aldridge
    Nov 19 '18 at 12:23











  • Yes I did it, that's why i checked the migration version present

    – Goamaral
    Nov 19 '18 at 12:42













  • Have you dropped test environment and recreate then reran?

    – Austio
    Nov 19 '18 at 12:46











  • @Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

    – Goamaral
    Nov 19 '18 at 12:53











  • @Austio Want the question rewards? Or should I answer my own question?

    – Goamaral
    Nov 19 '18 at 12:54



















  • Perhaps RAILS_ENV=test rails db:migrate

    – David Aldridge
    Nov 19 '18 at 12:23











  • Yes I did it, that's why i checked the migration version present

    – Goamaral
    Nov 19 '18 at 12:42













  • Have you dropped test environment and recreate then reran?

    – Austio
    Nov 19 '18 at 12:46











  • @Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

    – Goamaral
    Nov 19 '18 at 12:53











  • @Austio Want the question rewards? Or should I answer my own question?

    – Goamaral
    Nov 19 '18 at 12:54

















Perhaps RAILS_ENV=test rails db:migrate

– David Aldridge
Nov 19 '18 at 12:23





Perhaps RAILS_ENV=test rails db:migrate

– David Aldridge
Nov 19 '18 at 12:23













Yes I did it, that's why i checked the migration version present

– Goamaral
Nov 19 '18 at 12:42







Yes I did it, that's why i checked the migration version present

– Goamaral
Nov 19 '18 at 12:42















Have you dropped test environment and recreate then reran?

– Austio
Nov 19 '18 at 12:46





Have you dropped test environment and recreate then reran?

– Austio
Nov 19 '18 at 12:46













@Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

– Goamaral
Nov 19 '18 at 12:53





@Austio I ran rails db:drop, rails db:create and rails db:migrate which applies changes to development and test (At least the terminal logs suggest so). Apparently, it is not that simple, lol... I ran the above commands with the RAILS_ENV=test prefix and it solved it

– Goamaral
Nov 19 '18 at 12:53













@Austio Want the question rewards? Or should I answer my own question?

– Goamaral
Nov 19 '18 at 12:54





@Austio Want the question rewards? Or should I answer my own question?

– Goamaral
Nov 19 '18 at 12:54












1 Answer
1






active

oldest

votes


















1














I recommended dropping the test db and recreating it. Although these should remain in sync they sometimes do not and you have to do manual steps to get them together.



RAILS_ENV=test rake db:reset



Weird that it is not working with that, would be interesting to drop a pry debugger in that task and see what is going on. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122



Manual steps



RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate





share|improve this answer


























  • I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

    – Goamaral
    Nov 19 '18 at 16:02











  • Updated, super weird that is not working

    – Austio
    Nov 19 '18 at 16:09











  • Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

    – Goamaral
    Nov 19 '18 at 16:26






  • 1





    Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

    – Goamaral
    Nov 19 '18 at 16:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374130%2fmigration-with-sql-sequence-is-not-applied-in-test-environment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I recommended dropping the test db and recreating it. Although these should remain in sync they sometimes do not and you have to do manual steps to get them together.



RAILS_ENV=test rake db:reset



Weird that it is not working with that, would be interesting to drop a pry debugger in that task and see what is going on. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122



Manual steps



RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate





share|improve this answer


























  • I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

    – Goamaral
    Nov 19 '18 at 16:02











  • Updated, super weird that is not working

    – Austio
    Nov 19 '18 at 16:09











  • Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

    – Goamaral
    Nov 19 '18 at 16:26






  • 1





    Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

    – Goamaral
    Nov 19 '18 at 16:30
















1














I recommended dropping the test db and recreating it. Although these should remain in sync they sometimes do not and you have to do manual steps to get them together.



RAILS_ENV=test rake db:reset



Weird that it is not working with that, would be interesting to drop a pry debugger in that task and see what is going on. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122



Manual steps



RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate





share|improve this answer


























  • I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

    – Goamaral
    Nov 19 '18 at 16:02











  • Updated, super weird that is not working

    – Austio
    Nov 19 '18 at 16:09











  • Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

    – Goamaral
    Nov 19 '18 at 16:26






  • 1





    Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

    – Goamaral
    Nov 19 '18 at 16:30














1












1








1







I recommended dropping the test db and recreating it. Although these should remain in sync they sometimes do not and you have to do manual steps to get them together.



RAILS_ENV=test rake db:reset



Weird that it is not working with that, would be interesting to drop a pry debugger in that task and see what is going on. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122



Manual steps



RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate





share|improve this answer















I recommended dropping the test db and recreating it. Although these should remain in sync they sometimes do not and you have to do manual steps to get them together.



RAILS_ENV=test rake db:reset



Weird that it is not working with that, would be interesting to drop a pry debugger in that task and see what is going on. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L122



Manual steps



RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:migrate






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 16:08

























answered Nov 19 '18 at 15:23









AustioAustio

4,5171228




4,5171228













  • I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

    – Goamaral
    Nov 19 '18 at 16:02











  • Updated, super weird that is not working

    – Austio
    Nov 19 '18 at 16:09











  • Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

    – Goamaral
    Nov 19 '18 at 16:26






  • 1





    Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

    – Goamaral
    Nov 19 '18 at 16:30



















  • I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

    – Goamaral
    Nov 19 '18 at 16:02











  • Updated, super weird that is not working

    – Austio
    Nov 19 '18 at 16:09











  • Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

    – Goamaral
    Nov 19 '18 at 16:26






  • 1





    Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

    – Goamaral
    Nov 19 '18 at 16:30

















I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

– Goamaral
Nov 19 '18 at 16:02





I tried RAILS_ENV=test rake db:reset and it doesnt work. The only way, I managed to make it work is literally do what reset does but in seperated commands. RAILS_ENV=test rake db:drop then RAILS_ENV=test rake db:create then RAILS_ENV=test rake db:migrate

– Goamaral
Nov 19 '18 at 16:02













Updated, super weird that is not working

– Austio
Nov 19 '18 at 16:09





Updated, super weird that is not working

– Austio
Nov 19 '18 at 16:09













Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

– Goamaral
Nov 19 '18 at 16:26





Whats the difference between line 122 and line 185(At github.com/rails/rails/blob/master/activerecord/lib/…)? Dont they represent the same command?

– Goamaral
Nov 19 '18 at 16:26




1




1





Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

– Goamaral
Nov 19 '18 at 16:30





Apparently, RAILS_ENV=test rake db:reset doesn't do the same as RAILS_ENV=test rails db:reset

– Goamaral
Nov 19 '18 at 16:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374130%2fmigration-with-sql-sequence-is-not-applied-in-test-environment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents