staudenmeir / laravel-migration-views

Laravel database migrations with SQL views

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Request] Add option to get view columns list

enniosousa opened this issue · comments

It's possible get tables' columns as mencioned here https://stackoverflow.com/a/37157879/4830771

But it doesn't work with views on SQL Server. I had not tested on others database.

I'll look into it.

SQL Server is indeed the only database where getColumnListing() doesn't work for views.

The latest release adds getViewColumnListing() (requires Laravel 6):

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

$columns = Schema::getViewColumnListing('active_users');

Thanks! After I update to v1.3 with update d318a82 I got a error:

php artisan tinker
>>> Staudenmeir\LaravelMigrationViews\Facades\Schema::getViewColumnListing('app_series')
Illuminate/Database/QueryException with message 'SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The multi-part identifier "objects.object_id" could not be bo
und. (SQL: select columns.name from sys.columns
                join sys.objects on objects.object_id = columns.object_id
                where objects.type = 'V' and objects.name = app_series)'
>>> 

I'm using Microsoft SQL Server 2008 R2

Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
	Apr  2 2010 15:48:46 
	Copyright (c) Microsoft Corporation
	Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

For me, the following query works:

select 
    schema_name(views.schema_id) as schema_name
    , object_name(columns.object_id) as view_name
    , columns.column_id as id
    , columns.name
    , type_name(user_type_id) as data_type
    , columns.max_length
    , columns.precision 
from sys.columns as columns 
inner join sys.views as views on views.object_id = columns.object_id 
where object_name(columns.object_id) = 'app_series' 
order by schema_name asc, view_name asc, column_id asc

Does this query work?

select columns.name from sys.columns columns
join sys.objects objects on objects.object_id = columns.object_id
where objects.type = 'V' and objects.name = 'app_series'

Yes! It works.

Just add table alias
facepalm

Thanks, I've released a new version.

Interesting that older versions SQL Server behave differently here.

Now it works perfectly. Thank you so much!