Error: Connection terminated when creating a workspace

Hi, I am new here and after installing the self-hosted docker and creating a workspace like this:
const activatedWorkspace = await this.workspaceService.activateWorkspace(
user,
workspace,
{
displayName: options.workspaceName,
},
);
const activatedWorkspace = await this.workspaceService.activateWorkspace(
user,
workspace,
{
displayName: options.workspaceName,
},
);
I see an error in my Nestjs backend
query failed: INSERT INTO "workspace_7uj0qhoizrxe5byfv0l97izsw"."viewField"("fieldMetadataId", "position", "isVisible", "size", "viewId", "aggregateOperation") VALUES ($1, $2, $3, $4, $5, DEFAULT), ($6, $7, $8, $9, $10, DEFAULT), ($11, $12, $13, $14, $15, DEFAULT), ($16, $17, $18, $19, $20, DEFAULT), ($21, $22, $23, $24, $25, DEFAULT), ($26, $27, $28, $29, $30, DEFAULT) -- ...
error: Error: Connection terminated
/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219
throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
^

QueryFailedError: Connection terminated
at PostgresQueryRunner.query (/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async InsertQueryBuilder.execute (/app/node_modules/typeorm/query-builder/InsertQueryBuilder.js:106:33)
at async createWorkspaceViews (/app/packages/twenty-server/dist/src/engine/workspace-manager/standard-objects-prefill-data/prefill-views.js:77:13)
at async /app/packages/twenty-server/dist/src/engine/workspace-manager/standard-objects-prefill-data/standard-objects-prefill-data.js:22:39
at async EntityManager.transaction (/app/node_modules/typeorm/entity-manager/EntityManager.js:73:28) {
...
driverError: Error: Connection terminated
at /app/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async PostgresQueryRunner.query
...
query failed: INSERT INTO "workspace_7uj0qhoizrxe5byfv0l97izsw"."viewField"("fieldMetadataId", "position", "isVisible", "size", "viewId", "aggregateOperation") VALUES ($1, $2, $3, $4, $5, DEFAULT), ($6, $7, $8, $9, $10, DEFAULT), ($11, $12, $13, $14, $15, DEFAULT), ($16, $17, $18, $19, $20, DEFAULT), ($21, $22, $23, $24, $25, DEFAULT), ($26, $27, $28, $29, $30, DEFAULT) -- ...
error: Error: Connection terminated
/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219
throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
^

QueryFailedError: Connection terminated
at PostgresQueryRunner.query (/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async InsertQueryBuilder.execute (/app/node_modules/typeorm/query-builder/InsertQueryBuilder.js:106:33)
at async createWorkspaceViews (/app/packages/twenty-server/dist/src/engine/workspace-manager/standard-objects-prefill-data/prefill-views.js:77:13)
at async /app/packages/twenty-server/dist/src/engine/workspace-manager/standard-objects-prefill-data/standard-objects-prefill-data.js:22:39
at async EntityManager.transaction (/app/node_modules/typeorm/entity-manager/EntityManager.js:73:28) {
...
driverError: Error: Connection terminated
at /app/node_modules/pg/lib/client.js:526:17
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async PostgresQueryRunner.query
...
Any help is appreciated,
2 Replies
Prastoin
Prastoin5mo ago
Hello there, you can't call the activate workspace on its own. I would rather consume the api directly What's your need overall ?
RonRonRon
RonRonRonOP5mo ago
My use case is to setup the workspace automatically. It works perfectly with the .5.x version and after upgrading to 1.x the Prefill standard objects is failing.
async run(
passedParams: string[],
options: WorkspaceSignupOptions,
): Promise<void> {
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(options.username)) {
throw new Error('Invalid email format for username');
}

// Check if user already exists
const existingUser = await this.userRepository.findOne({
where: { email: options.username },
});
if (existingUser) {
throw new Error(`User with email ${options.username} already exists`);
}

// Validate password
if (options.password.length < 8) {
throw new Error('Password must be at least 8 characters long');
}

// Hash password and prepare user data
const passwordHash = await hashPassword(options.password);
const newUserData = {
email: options.username,
firstName: options.adminFirstName,
lastName: options.adminLastName,
passwordHash,
locale: options.locale,
};

// Create workspace and user
const { user, workspace } =
await this.signInUpService.signUpOnNewWorkspace({
type: 'newUserWithPicture',
newUserWithPicture: newUserData,
});

// Complete onboarding
await this.completeOnboarding(user, workspace);

// Activate workspace
const activatedWorkspace = await this.workspaceService.activateWorkspace(
user,
workspace,
{ displayName: options.workspaceName },
);
if (!activatedWorkspace) {
throw new Error('Failed to activate workspace');
}

// Ensure user is part of the workspace
await this.userWorkspaceService.addUserToWorkspaceIfUserNotInWorkspace(
user,
workspace,
);
}
async run(
passedParams: string[],
options: WorkspaceSignupOptions,
): Promise<void> {
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(options.username)) {
throw new Error('Invalid email format for username');
}

// Check if user already exists
const existingUser = await this.userRepository.findOne({
where: { email: options.username },
});
if (existingUser) {
throw new Error(`User with email ${options.username} already exists`);
}

// Validate password
if (options.password.length < 8) {
throw new Error('Password must be at least 8 characters long');
}

// Hash password and prepare user data
const passwordHash = await hashPassword(options.password);
const newUserData = {
email: options.username,
firstName: options.adminFirstName,
lastName: options.adminLastName,
passwordHash,
locale: options.locale,
};

// Create workspace and user
const { user, workspace } =
await this.signInUpService.signUpOnNewWorkspace({
type: 'newUserWithPicture',
newUserWithPicture: newUserData,
});

// Complete onboarding
await this.completeOnboarding(user, workspace);

// Activate workspace
const activatedWorkspace = await this.workspaceService.activateWorkspace(
user,
workspace,
{ displayName: options.workspaceName },
);
if (!activatedWorkspace) {
throw new Error('Failed to activate workspace');
}

// Ensure user is part of the workspace
await this.userWorkspaceService.addUserToWorkspaceIfUserNotInWorkspace(
user,
workspace,
);
}
I see this code in the data source of the workspace:
extra: {
query_timeout: 10000,
// https://node-postgres.com/apis/pool
// TypeORM doesn't allow sharing connection pools between data sources
// So we keep a small pool open for longer if connection pooling patch isn't enabled
// TODO: Probably not needed anymore when connection pooling patch is enabled
idleTimeoutMillis: TWENTY_MINUTES_IN_MS,
max: 4,
allowExitOnIdle: true,
},
extra: {
query_timeout: 10000,
// https://node-postgres.com/apis/pool
// TypeORM doesn't allow sharing connection pools between data sources
// So we keep a small pool open for longer if connection pooling patch isn't enabled
// TODO: Probably not needed anymore when connection pooling patch is enabled
idleTimeoutMillis: TWENTY_MINUTES_IN_MS,
max: 4,
allowExitOnIdle: true,
},
Also - The mutation is running this exact command:
@Mutation(() => Workspace)
@UseGuards(UserAuthGuard, WorkspaceAuthGuard)
async activateWorkspace(
@Args('data') data: ActivateWorkspaceInput,
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
return await this.workspaceService.activateWorkspace(user, workspace, data);
}
@Mutation(() => Workspace)
@UseGuards(UserAuthGuard, WorkspaceAuthGuard)
async activateWorkspace(
@Args('data') data: ActivateWorkspaceInput,
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
return await this.workspaceService.activateWorkspace(user, workspace, data);
}
So should it be okay to run it manually?

Did you find this page helpful?