lunarphp / lunar

An open-source package that brings the power of modern headless e-commerce functionality to Laravel.
https://lunarphp.io
MIT License
2.57k stars 338 forks source link

Problem in database prefix #307

Open sajaddp opened 2 years ago

sajaddp commented 2 years ago

Hi, I set the prefix for the database. I also removed the prefix for ‍getcandy‍‍ tables in the config file. An error occurs while running the admin. The error is related to the incorrect table name.

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "order_addresses" does not exist
select
  COUNT(*) as count,
  "vss_order_addresses"."contact_email"
from
  "vss_order_addresses"
  left join order_addresses address_join on "vss_address_join"."contact_email" = "vss_order_addresses"."contact_email"
where
  "vss_order_addresses"."type" = billing
  and "vss_order_addresses"."created_at" between 2022 -05 -30 00: 00: 00
  and 2022 -06 -05 00: 00: 00
  and "vss_order_addresses"."contact_email" is not null
group by
  "vss_order_addresses"."id",
  "vss_order_addresses"."contact_email"

As you can see, the error is related to the wrong name in Table ‍order_addresses‍, which is generated in the getReturningCustomersPercentProperty function.

alecritson commented 2 years ago

Hi @sajaddp Can you post your config at all? On one side you've said you set the prefix but then also removed it?

sajaddp commented 2 years ago

Hi @alecritson,

### config/getcandy/database.php
    'table_prefix' => '',
### config/databse.php

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => 'vss_', ## <---- here
            'prefix_indexes' => true,
            'search_path' => 'public',
            'sslmode' => 'prefer',
        ],
alecritson commented 2 years ago

Thanks @sajaddp It looks like Laravel isn't considering this prefix when you manually have to set the table names. I'll try and have a look to see if I can find a solution :)

alecritson commented 2 years ago

Had another look at this and it might run a little deeper than first thought, will have a chat with @glennjacobs when he's back in!

ades4827 commented 1 year ago

any news? i have the same problem using database prefix on function: getCustomerGroupOrdersProperty

wychoong commented 3 months ago

for this issue, there are 2 approach to it

  1. use DB connection to resolve table name
    
      $table = Lunar\Facades\DB::connection()->table((new OrderAddress))

// this approach will use the database connection prefix in config/database.php



2. set the table_prefix config in lunar
      this let us continue to use `->getTable()`

problem: setting prefix in both files likely causing issue with migration where it will ends up with `{prefix}{prefix}table_name`