npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.56k stars 225 forks source link

Reverse engineering for view does not preserve nullability of columns #3148

Closed vancodocton closed 7 months ago

vancodocton commented 7 months ago

I scaffold view of table that select several instead of all columns of a table. However, the model of view does not persist the column nullability as SQL Server does when scaffolding.

I posted a question on SO: https://stackoverflow.com/questions/78273009/postgresql-view-column-not-null-is-not-persist-when-reverse-engineering-using-en

vancodocton commented 7 months ago

It is the problem of create view, not scaffolding tool. When I run:

select * from information_schema.columns where table_name = 'terminal';
select * from information_schema.columns where table_name = 'terminal_view';

I have result image

As we can see, that view columns is_nullable are YES, that is not the same as of table.

Following https://stackoverflow.com/questions/17301323/why-are-my-views-columns-nullable, we have a workaround is that update attnotnull to true of table pg_attribute to ensure the nullability of scaffolded view model.

UPDATE pg_attribute
SET attnotnull = true
WHERE attrelid = 'terminal_view'::regclass
roji commented 7 months ago

@vancodocton do you think there's anything to do here on the EFCore.PG side?

vancodocton commented 7 months ago

@roji I think it is not necessary.

roji commented 7 months ago

Thanks.