0

我刚刚在Windows 10上安装了Bash,安装了libmysqlclient-dev软件包,并运行了一个rake任务,该任务执行下面的查询以使用ActiveRecord :: Base.connection在mysql数据库上创建一个VIEW .execute创建VIEW的Mysql2语法错误

DROP TABLE IF EXISTS debtors_customer_balances; 
CREATE OR REPLACE VIEW debtors_customer_balances AS 
    SELECT 
    customer_id, 
    SUM(amount_cents) AS total_cents, 
    SUM(CASE 
      WHEN due_on >= CURDATE() 
      THEN amount_cents 
      ELSE 0 
      END) AS current_cents, 
    SUM(CASE 
      WHEN due_on >= (CURDATE() - INTERVAL 7 DAY) 
      AND due_on < CURDATE() 
      THEN amount_cents 
      ELSE 0 
      END) AS overdue7_cents, 
    SUM(CASE 
      WHEN due_on >= (CURDATE() - INTERVAL 14 DAY) 
      AND due_on < (CURDATE() - INTERVAL 7 DAY) 
      THEN amount_cents 
      ELSE 0 
      END) AS overdue14_cents, 
    SUM(CASE 
      WHEN due_on >= (CURDATE() - INTERVAL 30 DAY) 
      AND due_on < (CURDATE() - INTERVAL 14 DAY) 
      THEN amount_cents 
      ELSE 0 
      END) AS overdue30_cents, 
    SUM(CASE 
      WHEN due_on < (CURDATE() - INTERVAL 30 DAY) 
      THEN amount_cents 
      ELSE 0 
      END) AS overdue30_plus_cents 
    FROM 
    debtors_balances 
    GROUP BY 
    customer_id; 

然而,它抛出一个错误

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE OR REPLACE VIEW debtors_customer_balances AS SELECT ' at line 2: DROP TABLE IF EXISTS debtors_customer_balances; CREATE OR REPLACE VIEW debtors_customer_balances AS SELECT customer_id, etc...

我想不出什么导致这个作为查询运行在Mac罚款,它只是在bash窗户我似乎得到这个语法错误。

我使用的宝石mysql2(0.3.18)

回答

0

它是由第一线DROP TABLE IF EXISTS debtors_customer_balances;这实际上应该是DROP VIEW ...,而不是造成的。删除第一行,它应该工作。无论如何,你正在使用CREATE OR REPLACE VIEW debtors_customer_balances那么添加前DROP声明的含义是什么?

+0

修复了查询以便它可以运行,但不幸的是,DROP TABLE行存在的原因是因为在db:seed的末尾调用了此查询,并且出于某种原因,VIEWS作为TABLES添加到模式,因此当db:schema:load作为种子(db:reset)的一部分运行时,它会将这些视图创建为表,并在它到达此查询时发生错误。 –

+0

通过使用ActiveRecord :: SchemaDumper.ignore_tables = ['debtors_customer_balances','debtors_customer_entity_balances']修复了在架构中将VIEWS添加为架构中的问题。 –