From c3135d03a64407e80b921b33f7296c5590766254 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 17 Oct 2019 14:27:57 -0700 Subject: [PATCH] ent/dialect/sql/schema: set nullability of columns on scan Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/106 Reviewed By: alexsn Differential Revision: D17987588 fbshipit-source-id: f5f985d28bdd7379826dca35111c61f1c169d5c0 --- dialect/sql/schema/postgres.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/dialect/sql/schema/postgres.go b/dialect/sql/schema/postgres.go index 5f462c80b..fd07ff70b 100644 --- a/dialect/sql/schema/postgres.go +++ b/dialect/sql/schema/postgres.go @@ -93,15 +93,15 @@ func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Tabl // // Get PK and UNI columns of a table: // - // SELECT a.attname AS COLUMN, + // SELECT a.attname AS column, // format_type(a.atttypid, a.atttypmod) AS data_type, - // i.indisprimary AS PRIMARY, + // i.indisprimary AS primary, // i.indisunique AS unique - // FROM pg_index i - // join pg_attribute a - // ON a.attrelid = i.indrelid - // AND a.attnum = ANY ( i.indkey ) - // WHERE i.indrelid = '' :: regclass; + // FROM pg_index i + // join pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY (i.indkey) + // join pg_stat_user_tables t ON t.relid = i.indrelid + // WHERE t.schemaname = CURRENT_SCHEMA() + // AND i.indrelid = '
' :: regclass; // // column | data_type | primary | unique // --------+-----------+---------+-------- @@ -125,6 +125,9 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error { if err := rows.Scan(&c.Name, &c.typ, &maxlen, &nullable, &defaults); err != nil { return fmt.Errorf("scanning column description: %v", err) } + if nullable.Valid { + c.Nullable = nullable.String == "YES" + } switch c.typ { case "boolean": c.Type = field.TypeBool