Alter table doesn't work when timestamp column has a NOT NULL constraint #459
-
Same issue was already discussed in #436 but it is marked as solved but without a solution (other than drop the database and recreate without altering) I have a
and when I try to alter the table
I receive an error
I found out (as stated also in the previous discussion) that the issue is NOT NULL constraint for timestamp column. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hello @okrsnak, I took a closer look here on this one for and I believe I have a solution that I can share. It appears that either of the following should work for your needs: ALTER TABLE `news`
MODIFY COLUMN `id` varchar(256) NOT NULL,
MODIFY COLUMN `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
MODIFY COLUMN `updated_at` timestamp NOT NULL DEFAULT current_timestamp();
ALTER TABLE `news`
MODIFY COLUMN `id` varchar(256) NOT NULL,
MODIFY COLUMN `created_at` timestamp NOT NULL DEFAULT (now()),
MODIFY COLUMN `updated_at` timestamp NOT NULL DEFAULT (now()); Interestingly, I was wondering if you had come across this situation when using our deploy request process initially, and when testing that out myself I didn't encounter any issues, but I did observe that even though I only ran the single When that deploy request succeeded, I checked on your database and noticed that it didn't appear that you were using the deploy request process yet and your branch was still set as a development one, which would allow schema changes to be made directly on it. So I tested things out again this way and created the table and then ran the alter and I was able to encounter the same issue you shared: From there, I tested to see if making an alter on those other columns to use Instead, it seemed that to be successful in this situation we need to combine the changes all together into a single statement using either of the two approaches I shared above: In most cases, this wouldn't be necessary, but for some reason this situation requires it to be done in a combined manner currently. I'll likely file an issue internally sharing this since it could potentially be a bug on our end, but for now the above option should be a workable solution in a case like this. I would expect that if a table had more timestamp columns that they would also have to be included too. |
Beta Was this translation helpful? Give feedback.
-
Just want to jump in and say that I encountered this issue too. I am using DrizzleORM. -- my table already had these in:
`created_at` timestamp NOT NULL DEFAULT (now()),
`updated_at` timestamp NOT NULL DEFAULT (now())
-- SQL from drizzle, does not work:
ALTER TABLE `task_lists` ADD `slug` varchar(256);
ALTER TABLE `task_lists` ADD `owner_id` varchar(256);
-- working replacement based on this thread:
ALTER TABLE task_lists
MODIFY COLUMN `created_at` timestamp NOT NULL DEFAULT (now()),
MODIFY COLUMN `updated_at` timestamp NOT NULL DEFAULT (now()),
ADD slug varchar(256),
ADD `owner_id` varchar(256); Has this been filed as a bug? It's not ideal to have to manually fix migrations like this. |
Beta Was this translation helpful? Give feedback.
Hello @okrsnak,
I took a closer look here on this one for and I believe I have a solution that I can share.
It appears that either of the following should work for your needs:
Interestingly, I was wondering if you had come across this situation when using our deploy request process initiall…