Question

I can't use south and I don't have pgAdmin but I need to add new field to the model at the end of the model. I need to be:

new_field = models.CharField(max_length=200, blank=True, null=True)

I have ssh access and have psql user defined so I need syntax to add that field to the model db table. Does ALTER TABLE store_products ADD COLUMN description text; do the job? How to set max_length 200, blank and null to be true? postgresql 9.1, django 1.6.

Was it helpful?

Solution

I take blank=True and null=True to mean that empty strings and NULL values should be allowed. These are the defaults, you don't need to do anything extra.

For max_length=200 you could use varchar(200) instead of text:

ALTER TABLE store_products ADD COLUMN description varchar(200);

But I generally prefer the data type text in combination with a CHECK constraint:

ALTER TABLE store_products ADD COLUMN description text;
ALTER TABLE store_products ADD CONSTRAINT description_max200
CHECK (length(description) <= 200);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top