在 ruby-2.7 中显示 Sqlite3 视图的问题
Problem to display Sqlite3 View in ruby-2.7
我在显示我使用 ruby 创建的视图(在 sqlite3 中成功创建)时遇到问题。
下面是sqlite视图
CREATE VIEW members_ytd_totals AS
SELECT id AS MemberID,
first_name,
last_name,
ipu_dues.total AS TotalDues,
ipu_levy.total AS TotalLevy,
ipu_fines.total AS TotalFines,
ipu_donation.total AS TotalDonations,
ipu_registration.total AS TotalRegistrations
FROM ipu_profile
INNER JOIN
ipu_dues ON ipu_dues.id = ipu_profile.id
INNER JOIN
ipu_levy ON ipu_levy.id = ipu_profile.id
INNER JOIN
ipu_fines ON ipu_fines.id = ipu_profile.id
INNER JOIN
ipu_donation ON ipu_donation.id = ipu_profile.id
INNER JOIN
ipu_registration ON ipu_registration.id = ipu_profile.id;
通常我应该能够在 ruby 中使用简单的 select 语句来显示此视图,如
require 'sqlite3'
require 'sqlite_print_table'
db = SQLite3::Database.open 'database.db'
db.print_table("SELECT * FROM members_ytd_totals", guide_tolerance: 4, width_tolerance: 5, margin: 10)
但是我得到了发生数据库异常 - 不明确的列名:id
但是在 sqlite3 中,这个视图是正确创建的,没有错误,我可以在 Sqlite studio 中看到该视图。我只是无法在 ruby 中显示它。知道问题出在哪里吗?
谢谢
当您说我可以在 Sqlite studio 中看到视图时,您的意思是视图的创建没有任何错误。
但是您是否尝试过使用它,例如:
SELECT * FROM members_ytd_totals;
你会得到同样的错误。
使用别名,尤其是在涉及多个连接的情况下,总是使用table的name/alias限定列名以避免歧义:
CREATE VIEW members_ytd_totals AS
SELECT
p.id AS MemberID, p.first_name, p.last_name,
d.total AS TotalDues, l.total AS TotalLevy,
f.total AS TotalFines, n.total AS TotalDonations,
r.total AS TotalRegistrations
FROM ipu_profile p
INNER JOIN ipu_dues d ON d.id = p.id
INNER JOIN ipu_levy l ON l.id = p.id
INNER JOIN ipu_fines f ON f.id = p.id
INNER JOIN ipu_donation n ON n.id = p.id
INNER JOIN ipu_registration r ON r.id = p.id;
我在显示我使用 ruby 创建的视图(在 sqlite3 中成功创建)时遇到问题。
下面是sqlite视图
CREATE VIEW members_ytd_totals AS
SELECT id AS MemberID,
first_name,
last_name,
ipu_dues.total AS TotalDues,
ipu_levy.total AS TotalLevy,
ipu_fines.total AS TotalFines,
ipu_donation.total AS TotalDonations,
ipu_registration.total AS TotalRegistrations
FROM ipu_profile
INNER JOIN
ipu_dues ON ipu_dues.id = ipu_profile.id
INNER JOIN
ipu_levy ON ipu_levy.id = ipu_profile.id
INNER JOIN
ipu_fines ON ipu_fines.id = ipu_profile.id
INNER JOIN
ipu_donation ON ipu_donation.id = ipu_profile.id
INNER JOIN
ipu_registration ON ipu_registration.id = ipu_profile.id;
通常我应该能够在 ruby 中使用简单的 select 语句来显示此视图,如
require 'sqlite3'
require 'sqlite_print_table'
db = SQLite3::Database.open 'database.db'
db.print_table("SELECT * FROM members_ytd_totals", guide_tolerance: 4, width_tolerance: 5, margin: 10)
但是我得到了发生数据库异常 - 不明确的列名:id
但是在 sqlite3 中,这个视图是正确创建的,没有错误,我可以在 Sqlite studio 中看到该视图。我只是无法在 ruby 中显示它。知道问题出在哪里吗?
谢谢
当您说我可以在 Sqlite studio 中看到视图时,您的意思是视图的创建没有任何错误。
但是您是否尝试过使用它,例如:
SELECT * FROM members_ytd_totals;
你会得到同样的错误。
使用别名,尤其是在涉及多个连接的情况下,总是使用table的name/alias限定列名以避免歧义:
CREATE VIEW members_ytd_totals AS
SELECT
p.id AS MemberID, p.first_name, p.last_name,
d.total AS TotalDues, l.total AS TotalLevy,
f.total AS TotalFines, n.total AS TotalDonations,
r.total AS TotalRegistrations
FROM ipu_profile p
INNER JOIN ipu_dues d ON d.id = p.id
INNER JOIN ipu_levy l ON l.id = p.id
INNER JOIN ipu_fines f ON f.id = p.id
INNER JOIN ipu_donation n ON n.id = p.id
INNER JOIN ipu_registration r ON r.id = p.id;