Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric value out of range when inserting #80

Closed
pelmered opened this issue Mar 7, 2019 · 10 comments · May be fixed by #71
Closed

Numeric value out of range when inserting #80

pelmered opened this issue Mar 7, 2019 · 10 comments · May be fixed by #71
Labels

Comments

@pelmered
Copy link

pelmered commented Mar 7, 2019

I get this error message when I do a simple insert to a Point field:

SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field (SQL: insert into `addresses` (`type`, `address_line_1`, `address_line_2`, `city`, `postal_code`, `country`, `user_uuid`, `uuid`, `location`, `updated_at`, `created_at`) values (billing, Drottninggatan 11, , Stockholm, 111 51, SE, ad120958-31b9-44da-92a8-e990c20eac42, 5bfa2e46-8fa5-4fbc-b818-0066adc858b8, 59.3315493 18.0637367, 2019-03-07 17:07:33, 2019-03-07 17:07:33))

The only solutions I can find is to add SpatialTrait and $spatialFields, but I already have them. This the relevant parts of the model:

<?php 

class Address extends Model
{
    use SpatialTrait;

    protected $spatialFields = ['location'];

    public static function boot()
    {
        parent::boot();

        self::creating(function (Address $address) {

            if (
                empty($address->attributes['location']) &&
                $address->isComplete()
            ) {
                $geocoded = app('geocoder')->geocodeAddress($address);
                $address->attributes['location'] = new Point($geocoded['latitude'], $geocoded['longitude']);
            }
        });
    }

    public function isComplete()
    {
        return isset($this->type, $this->address_line_1, $this->city, $this->postal_code, $this->country);
    }
}

I have double and triple checked the code, but I can't find anything wrong.
Any ideas?

@pelmered
Copy link
Author

pelmered commented Mar 7, 2019

This is probably a duplicate of #70, right?

@pelmered pelmered changed the title Numeric value out of range when insering Numeric value out of range when inserting Mar 7, 2019
@pelmered
Copy link
Author

pelmered commented Mar 7, 2019

I managed to solve this by looking at the #71 PR.

I added this code:

                $point = new Point($geocoded['latitude'], $geocoded['longitude']);
                $address->geometries['location'] = $point;
                $address->attributes['location'] = new SpatialExpression($point);

This shouldn't be necessary, but it solves the problem temporarily.

@miclf
Copy link

miclf commented Mar 5, 2020

@pelmered Thanks for the workaround! I’ve just stumbled upon the same issue while trying to assign a default value to a Point when creating a model…

static::creating(function (self $model) {
    if (is_null($model->location)) {
        // Throws MySQL error: Numeric value out of range blah blah blah...
        $model->location = Point($defaultLatitude, $defaultLongitude);
    }
});

The code you isolated fixes the issue 👌

static::creating(function (self $model) {
    if (is_null($model->location)) {
        $point = Point($defaultLatitude, $defaultLongitude);
        $model->geometries['location'] = $point;
        $model->attributes['location'] = new SpatialExpression($point);
    }
});

@grimzy
Copy link
Owner

grimzy commented Mar 9, 2020

Thanks for looking at this! And sorry for responding so late.

I'll try to look at #71 soon and put all of this in a tested PR.

@M-Barari
Copy link

M-Barari commented Apr 19, 2020

i'm having the same problem. is there any way instead of those two line above? as in the tutorial just new Point($lat, $lng) should be enough.
here is my code:
my Setting model:

use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
protected $guarded = [];
protected $spatialFields = ['location'];
public function setSetting($data) {
$lat = $data['lat'];
$lng = $data['lng'];
$this->title = $data['title'];
$this->address = $data['address'];
$this->location = new Point($lat, $lng)
$this->save();
}

and my controller:
public function set(){
$data = $this->validateRequest();
$setting = Setting::firstOrNew(['id' => 1]);
$setting->setSetting($data);
}

and when i add "use SpatialTrait" to my model, i get this error:
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1582 Incorrect parameter count in the call to native function 'ST_GeomFromText'

@miclf
Copy link

miclf commented Apr 19, 2020

@M-Barari In your Setting model, try to replace this line

$this->location = new Point($lat, $lng);

by this

$point = Point($lat, $lng);
$this->geometries['location'] = $point;
$this->attributes['location'] = new \Grimzy\LaravelMysqlSpatial\EloquentSpatialExpression($point);

I can’t guarantee this will work (I cannot guess by just looking at your code) but if your issue is the same as the one described above, then it should help.

As for your PDOException: SQLSTATE[42000] error, I’m not sure if it’s related.

Welcome to GitHub, by the way. Here is a tip that you may find helpful : when writing code in comments, you can surround it by ` accents to display it with indentation and syntax highlighting. Like this:

```php
echo $hello;
```

You can replace the php part by the appropriate language if it’s different (html or css, for example). You can find more info in the GitHub help: https://guides.github.com/features/mastering-markdown/

@snorpey
Copy link

snorpey commented May 7, 2020

@M-Barari I ran into the same error and was able to fix it by installing v2.0 of the library. I'm running MySQL 5.7, which according to the readme should be used with v2.0.

@mostafakram
Copy link

Facing the same issue

when trying to save

{
    [
        {
            "lat": "30.590213",
            "lng": "31.519949"
        },
        {
            "lat": "30.589770",
            "lng": "31.521848"
        }
    ]
}

I get SQL error

SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field (SQL: update .... route_path = ST_GeomFromText(LINESTRING(31.519949 30.590213,31.521848 30.58977)), ....

i have tried the proposed solution and my code is

$lineString = new LineString($points);
$this->geometries['route_path'] = $lineString;
$this->attributes['route_path'] = new SpatialExpression($lineString);

but still not fixed

package version 2.2.3
php 7.4.2
mysql 5.7.30

@mostafakram
Copy link

Please ignore my comment

i believe the issue was because my migration was not correct

Thanks

@pelmered
Copy link
Author

pelmered commented Oct 1, 2020

I had this issue again now, but now it was because I didn't have SpatialTrait on my model.

I think this issue can be closed now.

@pelmered pelmered closed this as completed Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants