执行PostgreSQL脚本时出现FluentMigrator错误

时间:2017-05-23 13:58:52

标签: postgresql migration fluent-migrator

我正在尝试执行以下PostgreSQL代码

DECLARE f RECORD;
DECLARE p RECORD;
DECLARE idaus int = 0;
BEGIN   
     FOR f IN EXECUTE 'SELECT DISTINCT name_f FROM piece'
     LOOP 
         EXECUTE 'INSERT INTO format (name) VALUES('''|| f.name_f ::text||''')';
     END LOOP;

     EXECUTE 'ALTER TABLE piece ADD COLUMN format_id integer';

     FOR p IN EXECUTE 'SELECT DISTINCT name_f FROM piece'
     LOOP
         idaus := (SELECT format.id FROM format WHERE name = p.name_f );
         EXECUTE 'UPDATE piece SET format_id = '||idaus||' WHERE name_f = '''||p.name_f ::text||'''';
     END LOOP;

     EXECUTE 'ALTER TABLE piece ADD CONSTRAINT FK_PieceFormat FOREIGN KEY (format_id) REFERENCES format ON DELETE CASCADE;';

     EXECUTE 'ALTER TABLE piece DROP COLUMN name_f RESTRICT';
 END;

迁移期间

 var script = File.ReadAllText(FunctionFullPathName);
 Execute.Sql(script );

但我有以下错误:

42601: syntax error near or at "RECORD"

我读了这个https://github.com/fluentmigrator/fluentmigrator/wiki/Fluent-Interface,但我没有找到任何东西。有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

(代表OP发布)

我找到了这个解决方案;也许它不是最好的,但至少它是有效的。我在里面定义了一个带有该代码的函数,在创建它之后,我将其称为:

Execute.Sql("SELECT do_migration_004()");