Serverpod Flutter-Dart PostgreSql error when creating table and executing insert query through Serverpod API

205 views Asked by At

I have a Serverpod server, and I want to create some tables in the PostgreSQL database from port 8090. After I create the file in protocol/file.yaml and execute serverpod generate and create the endpoint that I will access, I access it through Postman. This is the YAML :

class: Test
table: test
fields:
  title: String
  content: String

And this is the endpoint class from where i get "addTest" function that i access through Postman/Flutter Android Client. The getTests method is just a select * on the database and the addTest is a method with what i insert into the database. Here is where i get the error, into the insert function. I searched all over the internet for a method to solve it but did not find anything:

import 'package:serverpod/serverpod.dart'; import 'package:test_bd_server/src/generated/protocol.dart';

  Future<List<Test>> getTests(Session session, {String? keyword}) async {
    final elem = await Test.find(session,
        where: (t) =>
            keyword != null ? t.title.like('%$keyword') : Constant(true));

    return elem;
  }

  Future<bool> addTest(
      Session session, String arg_title, String arg_content) async {
    Test test = Test(content: arg_content, title: arg_title);
    Test.insert(session, test);
    return true;
  }
}


This is the Postman GET request (i tried with POST, same story). Here the 2 arg_title and arg_content are strings and i just simulate the method trigger with postman because this is another project, not my main where i get the same errors:

{
    "method": "addTest",
    "arg_title": "titlu_test",
    "arg_content": "content_test"
}

After this request executes, I get this error in the serververpod server:

2023-11-03 07:44:47.840836Z Internal server error. Zoned exception.
PostgreSQLSeverity.error 42P01: relation "test" does not exist
package:postgres/src/connection.dart 520:18 _PostgreSQLExecutionContextMixin._query
package:postgres/src/connection.dart 481:7 _PostgreSQLExecutionContextMixin.query
package:postgres_pool/postgres_pool.dart 801:23 _PgExecutionContextWrapper.query.<fn>
package:postgres_pool/postgres_pool.dart 800:12 _PgExecutionContextWrapper.query
package:postgres_pool/postgres_pool.dart 615:16 PgPool.query.<fn>
package:postgres_pool/postgres_pool.dart 352:22 PgPool.run.<fn>.<fn>
package:postgres_pool/postgres_pool.dart 451:27 PgPool._useOrCreate
package:postgres_pool/postgres_pool.dart 423:14 PgPool._withConnection.<fn>
package:executor/src/executor_impl.dart 61:19 _Executor.scheduleTask
package:postgres_pool/postgres_pool.dart 351:18 PgPool.run.<fn>
package:retry/retry.dart 131:16 RetryOptions.retry
package:postgres_pool/postgres_pool.dart 349:14 PgPool.run
package:serverpod/src/database/database_connection.dart 363:11 DatabaseConnection.insert
package:serverpod/src/database/database.dart 137:5 Database.insert
===== asynchronous gap ===========================
package:postgres_pool/postgres_pool.dart 351:18 PgPool.run.<fn>
package:retry/retry.dart 131:16 RetryOptions.retry
package:postgres_pool/postgres_pool.dart 349:14 PgPool.run
package:serverpod/src/database/database_connection.dart 363:11 DatabaseConnection.insert
package:serverpod/src/database/database.dart 137:5 Database.insert

What am I doing wrong? Where is the mistake? In Postman, I get "true" after I execute this. Help me please :).

The idea is that i want from the flutter client to make a login interface and create an insert query. When i try to do this, i get that relation error. I do not know why. Even, as i said, I try to recreate another servers, i get the same error. In the beginning i got another relation error: Failed to connect to the database. Retrying in 10 seconds. PostgreSQLSeverity.error 42P01: relation "serverpod_runtime_settings" does not exist. In order to fix this error I executed some commands that i got from stackoverflow where i copied the tables inside my postgres container that i host on docker desktop. I want to say the fact that i run serverpod and docker desktop from Windows 10 Pro. I have to say the fact that right now i cannot change the server idea. I have to stick to Serverpod. That's why i do not change the server :)

1

There are 1 answers

0
Jonas Öhrn On BEST ANSWER

You forgot to apply the generated script to create the table "test". See Apply database changes on the Build your first app. After I did that for your code above, it worked just fine :-)

Also, for consistency you should name your protocol file "test.yaml" instead of "file.yaml". Good luck!