I am running PostgreSQL 9.2 on Windows.
I have an existing table with some non-null columns:
CREATE TABLE testtable ( bkid serial NOT NULL, bklabel character varying(128), lacid integer NOT NULL }
I create a view in this table:
CREATE OR REPLACE VIEW test AS SELECT testtable.bkid, testtable.lacid from public.testtable;
I am surprised that in reports report_schema.columns for reports of the form is_nullable is YES for the selected columns?
select * from information_schema.columns where table_name = 'test'
Reports:
"MyDatabase";"public";"test";"bkid";1;"";"YES";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"1";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO" "MyDatabase";"public";"test";"lacid";2;"";"YES";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"2";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO"
Is this expected behavior?
My problem is that I am trying to import such views into the Entity Framework data model, and it fails because all columns are marked as nullable.
EDIT 1 :
The following query:
select attrelid, attname, attnotnull, pg_class.relname from pg_attribute inner join pg_class on attrelid = oid where relname = 'test'
returns:
attrelid;attname;attnotnull;relname 271543;"bkid";f;"test" 271543;"lacid";f;"test"
As expected, attnotnull is "false."
As Mike-Sherrill-Katkoll suggested, I could manually set them to true:
update pg_attribute set attnotnull = 't' where attrelid = 271543
And this change is reflected in information_commands:
select * from information_schema.columns where table_name = 'test'
Output:
"MyDatabase";"public";"test";"bkid";1;"";"NO";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"1";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO" "MyDatabase";"public";"test";"lacid";2;"";"NO";"integer";;;32;2;0;;"";;"";"";"";"";"";"";"";"";"";"MyDatabase";"pg_catalog";"int4";"";"";"";;"2";"NO";"NO";"";"";"";"";"";"";"NEVER";"";"NO"
I will try to import views into the Entity Framework data model.
EDIT 2 :
As already mentioned, it works, the view is now correctly imported into the Entity Framework data model. Of course, I will not set all columns to be invalid, as shown above, only those that cannot be zeroed out in the base table.