Skip to content

Instantly share code, notes, and snippets.

@hatemogi
Created January 25, 2023 05:53
Show Gist options
  • Save hatemogi/422485f62ba28ea2957bc68ec7b554bf to your computer and use it in GitHub Desktop.
Save hatemogi/422485f62ba28ea2957bc68ec7b554bf to your computer and use it in GitHub Desktop.
PG's view columns do not follow "NOT NULL" from the original table's
-- PostgreSQL에서 view를 생성하면, 해당 view의 컬럼메타데이터 중에, null여부가 잘 저장되지 않는 문제.
-- 좀 찜찜하지만, 뷰 생성 후에 view의 metadata를 수동으로 업데이트 해서, NOT NULL 정보를 업데이트 할 수 있음.
CREATE VIEW a_view_from_table AS
SELECT *
FROM original_table
; -- WHERE {conditions}
UPDATE pg_attribute
SET attnotnull=true
WHERE attrelid=(SELECT oid FROM pg_class WHERE relname='a_view_from_table')
AND attname IN (SELECT column_name FROM information_schema.columns WHERE table_name='original_table' AND is_nullable='NO');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment