Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Columns in create table default to nullable #33

Open
japettyjohn opened this issue Feb 10, 2015 · 2 comments
Open

Columns in create table default to nullable #33

japettyjohn opened this issue Feb 10, 2015 · 2 comments

Comments

@japettyjohn
Copy link

Column definitions should be non-nullable for native non pointer types.

When creating new tables the columns for non-pointer types are nullable in the database (at least for MariaDB). In the case that inserts/updates only goes through the API this is can be negated in terms a data integrity issue - but this cannot be guaranteed and it will certainly cause problems with the API to assign nulls to non-nilable fields.

From the performance end it may be implementation specific but mysql is faster with non-nullable columns (http://stackoverflow.com/questions/471367/when-to-use-null-in-mysql-tables).

This is addressed in the createTable of https://github.com/jmoiron/modl/blob/master/dbmap.go#L180 and is dependant on the ColumnMap here https://github.com/jmoiron/modl/blob/master/tablemap.go#L295 .

@chowey
Copy link

chowey commented Aug 16, 2015

I also would prefer that a non-pointer type is not nullable by default. This makes sense for data integrity.

However there is a counter-example with the Null* types in database/sql (e.g. NullString, NullBool, etc). These are not pointers but are clearly nullable.

There are two possible solutions:

  1. If a field is not a pointer type, AND it does not implement sql.Scanner, then it should default to "not null" in the create table statement. Such a field will not correctly handle a null value anyway.
  2. Provide an option on ColumnMap, something like columnMap.SetNullable(false). Then I can do whatever I want regardless.

I think both solutions should be done.


Currently I am just using columnMap.SetSqlCreate() and specifying it all manually.

@japettyjohn
Copy link
Author

I've addressed this and the dialects additionally regarding ptrs etc. in #42.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants