Test Error: Base table or view not found: 1146 Table 'testing.users' doesn't exist - laravel

I make this simple test:
public function test_my_awesome_test()
{
$user = User::find(1);
$response = $this->actingAs($user)->get('/awesome/test');
$response->assertStatus(200);
}
But after run test it's display me error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'testing.users' doesn't exist (SQL: select * from users where
users.id = 1 limit 1)
I had this same error if I needed to print some data from database. This tables exist and website works correctly. Who know where is problem?

According to the Laravel documentation, Sail overrides the DB name you defined in the .env file when you run the tests. You need to remove the following line from your phpunit.xml file:
<env name="DB_DATABASE" value="testing"/>

make .testing file in root of project and set database connection config like .env file in that . if you run a test , laravel use .testing instead of .env to read enviroment

Related

Sending a `create` RPC message to SurrealDB returns a "There was a problem with the database: The table does not exist" error

I'm debugging some tests for the .NET SurrealDB library. I can open connections to the database just fine but when I send a create RPC message to the db (docker container) it returns an error that reads "There was a problem with the database: The table does not exist"
TRACE tungstenite::protocol Received message {"id":"02B70C1AFE5D","async":true,"method":"create","params":["users",{"username":"john","password":"test123"}]}
...
16 13:46:45] DEBUG surrealdb::dbs Executing: CREATE $what CONTENT $data RETURN AFTER
surreal_1 | [2022-09-16 13:46:45] TRACE surrealdb::dbs Iterating: CREATE $what CONTENT $data RETURN AFTER
code: -32000, message: "There was a problem with the database: The table does not exist"
Any idea why that would happen? The table, of course, doesn't exist since I'm trying to create it. Would there be another reason in the Surreal code that such an error would be returned?
The error message was a red herring. The actual issue was that the client had an error that didn't allow it to sign in correctly so it wasn't authorized to make changes to the database.
Offending code:
// The table doesn't exist
Err(Error::TbNotFound) => match opt.auth.check(Level::Db) {
// We can create the table automatically
true => {
run.add_and_cache_ns(opt.ns(), opt.strict).await?;
run.add_and_cache_db(opt.ns(), opt.db(), opt.strict).await?;
run.add_and_cache_tb(opt.ns(), opt.db(), &rid.tb, opt.strict).await
}
// We can't create the table so error
false => Err(Error::TbNotFound), // Wrong Error Message
},
This has since been fixed and should now return a query permission error if the client is unauthorized.

Laravel Seeder error Cannot write to directory "public/storage/.."

I got this DatabaseSeeder.php in laravel where I create a Directory:
public function run()
{
Storage::deleteDirectory('posts');
Storage::makeDirectory('posts');
// Etcetera ...
}
And then I run
$ php artisan migrate:fresh --seed
This works flawlessly in my development environment (Laravel Homestead).
However, if I run that same command inside a Docker container using Laradock, I get the following error:
Seeding: Database\Seeders\PostsSeeder
InvalidArgumentException
Cannot write to directory "public/storage/posts"
at vendor/fakerphp/faker/src/Faker/Provider/Image.php:90
86▕ ) {
87▕ $dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
88▕ // Validate directory path
89▕ if (!is_dir($dir) || !is_writable($dir)) { ➜ 90▕ throw new \InvalidArgumentException(sprintf('Cannot write to directory
"%s"', $dir));
91▕ }
92▕
93▕ // Generate a random filename. Use the server address so that a file
94▕ // generated at the same time on a different server won't have a collision.
+3 vendor frames
database/factories/ImageFactory.php:25
Faker\Generator::__call()
Why is this happening?
How do I fix it?
Workaround
To easily solve it simply create those directories:
$ ls -lah public/
$ cd public/ && mkdir storage && cd storage && mkdir posts && cd ../..
Try again
$ artisan migrate:refresh --seed
This error is because one of your "Factory" files, in the "faker" method in this case, the "posts" folder does not exist.
Faker does not have the ability to create a new folder and since the folder does not exist, I get that error.
So in this case you have to create the posts folder first. To do this, inside your "DatabaseSeeder.php" file, place the following line of code inside the run () method at the beginning:
use Illuminate\Support\Facades\Storage;
public function run()
{
Storage::deleteDirectory('public/posts');
Storage::makeDirectory('public/posts');
...
}
I had the same problem, I create the folder using the workaround, and it works, but running artisan migrate: refresh --seed doesn't remove the directories but creates more.
public function run()
{
Storage::deleteDirectory('posts');
Storage::makeDirectory('posts');
// Etcetera ...
}

Laravel adds mysql in file path

I am facing a weird issue with Laravel 5.5. I am working on my local xampp server and trying to execute LOAD DATA INFILE query in controller function. But I am getting error PDOException (HY000)
SQLSTATE[HY000]: General error: 29 File 'D:\xampp\mysql\data\uploads\RDMST.csv' not found (Errcode: 2 "No such file or directory")
Currently my csv file is located at public/uploads folder. I tried various path settings for filename like
$fileCsv = 'uploads/RDMST.csv';
$fileCsv = public_path().'/uploads/RDMST.csv';
But I am getting same error and please notice the error path shows mysql\data in path. I am not getting from where it is appending this two folders in path. When I echo public path , it displays this:
D:\xampp\htdocs\tax\public
So, can you please help how to use path for load file without appending this "mysql\data" in path?
Any help is greatly appreciated.
Here is my complete controller function:
public function getCsv()
{
//echo public_path(); die;
//$fileCsv = public_path().'/uploads/RDMST.csv';
$fileCsv = 'uploads/RDMST.csv';
echo $query = "LOAD DATA INFILE '".$fileCsv."'
REPLACE
INTO TABLE tblreception
(year,
reception_number,
file_date,
file_time,
instrument_type,
#created_at,
#updated_at)
SET created_at=NOW(),updated_at=null";
DB::connection()->getPdo()->exec($query);
//return view('reception');
}
Can we not use csv file path?
Thanks

CI_session and Error Number: 1054 (Unknown column ...)

I have got problem with ci_session. When I am creating query with Active Record and I use session, it throws me Error Number: 1054.
It is caused by Session.php . I am using database session and when it executes sess_write(), then it throws error.
Can I create new instance of $this->db ?
Error is at line no. 289 in Session.php
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
In this place is set $this->CI->db->order_by(‘something’), from my code, before I call $this->session and this is my problem.
EDIT (13:12):
Unknown column 'contract.created' in 'order clause'
UPDATE `ci_sessions` SET `last_activity` = 1393330303, `user_data` = 'some_data' WHERE `session_id` = 'ea330d7194f902b6b38b88d509766560' ORDER BY `contract`.`created` DESC LIMIT 30
Filename: C:\Apache24\htdocs\Trokadero_v5\system\database\DB_driver.php
EDIT 2:
I am sure, it is happening by Active Record , because sequence of AR tasks is:
My_controller.php
$this->db->order_by('contract.created', 'DESC');
$this->session->set_flashdata('order_by_direction', 'DESC');
And then is executing in Session.php, with my own order_by clause
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
I got it.
$this->session->userdata();
$this->session->set_userdata();
etc.
MUST BE used before/after execute your own CRUD ($this->db->...).
In your controller, you have to reset the db query.
$this->db->order_by('contract.created', 'DESC');
// add line of code to get data. After getting data, reset the db object.
$this->db->_reset_write();
$this->session->set_flashdata('order_by_direction', 'DESC');

Creating a table from a schema? Using Ruby Sequel gem

I can dump the current Database#schema, but I'd like to create a new table (in a different database) with that schema, but I dont see how this can be done?
DB = Sequel.connect('sqlite:///database_from.db')
schema = DB.schema :table
# schema => [[:id, {:auto_increment=>true, :allow_null=>false, :default=>nil, :primary_key=>true, :db_type=>"smallint(5) unsigned", :type=>:integer, :ruby_default=>nil}], [:field, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"smallint(5) unsigned", :type=>:integer, :ruby_default=>nil}]]
DB2 = Sequel.connect('sqlite:///database_to.db')
DB2.create_table('table name', schema) #< allowing this would be cool!
One way it can be done via Migrations
Dump the copy from database :-
sequel -d mysql://root#localhost/database1 > db/001_test.rb
Edit to only include the table required.
Run the migration into the new db :-
sequel -m db/ mysql://root#localhost/database2

Resources